PHP getdate() function



PHP getdate() function

The PHP getdate() function is used to returns date time. The getdate() function return an array with all the date and time values.

The PHP getdate() function syntax:

getdate( timestamp);

The time stamp is optional. without time stamp the function will return current local date and time values. We use getdate() function for comparison between two dates.

Example of PHP getdate() function

Here, are the lists of elements contained in array returned by getdate() function in php.

  • mday – for day of the month (1 – 31)
  • wday – for dat of the week (1 – 6)
  • yday – for day of the year (0 – 365)
  • mon – for month of year in digit (1 – 12)
  • month – for month of year in string (January)
  • year – for year in four digit (2015)
  • hours – for display hours of day (0 – 23)
  • minutes – for display minutes of hour (0 – 59)
  • seconds – for display seconds of minutes (0 – 59
  • weekday – for day of the week (Monday)
  • 0 – for timestamp

Example of PHP getdate() function

<?php

$today=getdate();

echo "The Day of month = ". $today['mday'] . "<br>";
echo "Day of week = ". $today['wday'] . "<br>";
echo "Day of year = ". $today['yday'] . "<br>";
echo "Month of year in digit = ". $today['mon'] . "<br>";
echo "Month of year = ". $today['month'] . "<br>";
echo "Year is = ". $today['year'] . "<br>";
echo "Hours is = ". $today['hours'] . "<br>";
echo "Minutes is = ". $today['minutes'] . "<br>";
echo "Seconds is = ". $today['seconds'] . "<br>";
echo "Day of week is = ". $today['weekday'] . "<br>";
echo "Timestamp is = ". $today['0'] . "<br>";

?>

The output of getdate() function example :

The Day of month = 20
Day of week = 6
Day of year = 232
Month of year in digit = 8
Month of year = August
Year is = 2016
Hours is = 6
Minutes is = 59
Seconds is = 10
Day of week is = Saturday
Timestamp is = 1471669150


getdate() function example :

Now, lets an example to calculate difference between two dates using getdate() function.
Here, we assume some value for day in digit and get current day of year value from getdate() function then subtracts day value from current day of year value, you will get difference of two dates in result.

<?php

$day = 50;
$today_date=getdate();

$c_day=$today_date['yday'];

$day_diff = $c_day-$day;

echo "The Result is = ". $day_diff;

?>

The output is :

The Result is = 182

Leave a Reply

Your email address will not be published. Required fields are marked *