Getting Various Dates using PHP Date Function

In this more complete date/time PHP tutorial, I will show you how to use the PHP getdate function. I will also demonstrate how you can get various dates from the PHP date object. For example, you can do the following to get both today and yesterday's date on any given date and time:

$d1 = date("Y-m-d");//today's date.
echo "Today: $d1<br />";
$d2 = date("Y-m-d", strtotime("yesterday"));//yesterday's date
echo "Yestarday: $d2<br />";

PHP's getdate Function:

Simply using the following line of code below will return an array with elements including various date/time values as listed in the table below:

$info = getdate();

Elements available in the returned array
Key Description Example  values
"seconds" Numeric representation of seconds 0 to 59
"minutes" Numeric representation of minutes 0 to 59
"hours" Numeric representation of hours 0 to 23
"mday" Numeric representation of the day of the month 1 to 31
"wday" Numeric representation of the day of the week 0 (for Sunday) - 6 (for Saturday)
"mon" Numeric representation of a month 1 - 12
"year" Full numeric representation of a year, 4 digits I.E.: 1999 or 2003
"yday" Numeric representation of the day of the year 0 - 365
"weekday" Full textual representation of day of the week Sunday - Saturday
"month" A full textual representation of a month January - December
0 Seconds since Unix Epoch, similar to values returned by time() and used by date(). Depending on OS, it is normally from -2147483648 to 2147483647.

Now given what you learned here and combine it with some other PHP date/time functions and you can do about anything with dates and times in PHP. I've put together a little demo script that shows a lot of the more useful functions and demonstrates how to use them. Consider the following demo code:

<?php

$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];

//prepend a zero to the date if it is less than 10:
if($date < 10) $date = "0$date";

$current_date = $month."-".$date."-".$year."_".$hour."-".$min."-".$sec;

echo "<p>current getdate time: $current_date</p>";

//now some strtotime date/time examples:
echo "<h3>strtotime unix timestamps</h3>";
echo strtotime ("+1 day"), "<br>";
echo strtotime ("+1 week"), "<br>";
echo strtotime ("+1 week 2 days 4 hours 2 seconds"), "<br>";
echo strtotime ("next Thursday"), "<br>";
echo strtotime ("last Monday"), "<hr>";

echo "<h3>Examples of strtotime used with date:</h3>";
$d=strtotime("tomorrow");
echo "tomorrow: ".date("Y-m-d h:i:sa", $d) . "<br>";

$d=strtotime("yesterday");
echo "yesterday: ".date("Y-m-d h:i:sa", $d) . "<br>";

$d=strtotime("next Saturday");
echo "next Saturday: ". date("Y-m-d h:i:sa", $d) . "<br>";

$d=strtotime("+3 Months");
echo "+3 Months:".date("Y-m-d h:i:sa", $d) . "<br>";
?>

PHP's date function Parameters:

Here's a complete list of the parameters you can use inside the parenthesis with the date function:

  • d - The day of the month (from 01 to 31)
  • D - A textual representation of a day (three letters)
  • j - The day of the month without leading zeros (1 to 31)
  • l (lowercase 'L') - A full textual representation of a day
  • N - The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
  • S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
  • w - A numeric representation of the day (0 for Sunday, 6 for Saturday)
  • z - The day of the year (from 0 through 365)
  • W - The ISO-8601 week number of year (weeks starting on Monday)
  • F - A full textual representation of a month (January through December)
  • m - A numeric representation of a month (from 01 to 12)
  • M - A short textual representation of a month (three letters)
  • n - A numeric representation of a month, without leading zeros (1 to 12)
  • t - The number of days in the given month
  • L - Whether it's a leap year (1 if it is a leap year, 0 otherwise)
  • o - The ISO-8601 year number
  • Y - A four digit representation of a year
  • y - A two digit representation of a year
  • a - Lowercase am or pm
  • A - Uppercase AM or PM
  • B - Swatch Internet time (000 to 999)
  • g - 12-hour format of an hour (1 to 12)
  • G - 24-hour format of an hour (0 to 23)
  • h - 12-hour format of an hour (01 to 12)
  • H - 24-hour format of an hour (00 to 23)
  • i - Minutes with leading zeros (00 to 59)
  • s - Seconds, with leading zeros (00 to 59)
  • u - Microseconds (added in PHP 5.2.2)
  • e - The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
  • I (capital i) - Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
  • O - Difference to Greenwich time (GMT) in hours (Example: +0100)
  • P - Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
  • T - Timezone abbreviations (Examples: EST, MDT)
  • Z - Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
  • c - The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
  • r - The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
  • U - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

Since PHP 5.1.0, the following constants can also be used with the date function:

  • DATE_ATOM - Atom (example: 2013-04-12T15:52:01+00:00)
  • DATE_COOKIE - HTTP Cookies (example: Friday, 12-Apr-13 15:52:01 UTC)
  • DATE_ISO8601 - ISO-8601 (example: 2013-04-12T15:52:01+0000)
  • DATE_RFC822 - RFC 822 (example: Fri, 12 Apr 13 15:52:01 +0000)
  • DATE_RFC850 - RFC 850 (example: Friday, 12-Apr-13 15:52:01 UTC)
  • DATE_RFC1036 - RFC 1036 (example: Fri, 12 Apr 13 15:52:01 +0000)
  • DATE_RFC1123 - RFC 1123 (example: Fri, 12 Apr 2013 15:52:01 +0000)
  • DATE_RFC2822 - RFC 2822 (Fri, 12 Apr 2013 15:52:01 +0000)
  • DATE_RFC3339 - Same as DATE_ATOM (since PHP 5.1.3)
  • DATE_RSS - RSS (Fri, 12 Aug 2013 15:52:01 +0000)
  • DATE_W3C - World Wide Web Consortium (example: 2013-04-12T15:52:01+00:00)

Leave a Reply

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