Date Time Function in PHP
To keep track of the date and time, PHP uses standard Unix timestamps, which are simply the number of seconds elapsed since midnight, January 1, 1970.
To determine the current timestamp, you can use the time function:
Because the value is stored as seconds, to obtain the timestamp for this time next week you would use the following, which adds 7 days × 24 hours × 60 minutes × 60 seconds to the returned value:
echo time() + 7 * 24 * 60 * 60
If you wish to create a timestamp for a given date, you can use the mktime function.
echo mktime(0, 0, 0, 1, 1, 2000);
The parameters to pass are, in order from left to right:
1. The number of the hour (0–23)
2. The number of the minute (0–59)
3. The number of seconds (0–59)
4. The number of the month (1–12)
5. The number of the day (1–31)
6. The year (1970–2038, or 1901–2038 with PHP 5.1.0+ on 32-bit signed systems)
To display the date, use the date function. This function supports a plethora of formatting options,
enabling you to display the date any way you could wish. The format is as follows:
There are a number of useful constants that you can use with the date command to return the
date in specific formats.
For example, date(DATE_RSS) returns the current date and time in the valid format for an RSS feed. Some of the more commonly used constants are:
This is the format for Atom feeds. The PHP format is “Y-m-d\TH:i:sP” and example output is “2012-08-16T12:00:00+00:00”.
This is the format for RSS feeds. The PHP format is “D, d M Y H:i:s O” and example output is “Thu, 16 Aug 2012 12:00:00 UTC”.
This is the format for the World Wide Web Consortium. The PHP format is “Ym-d\TH:i:sP” and example output is “2012-08-16T12:00:00+00:00”.
<?php $month = 9; // September (only has 30 days) $day = 31; // 31st $year = 2012; // 2012 if (checkdate($month, $day, $year)) echo "Date is valid"; else echo "Date is invalid"; ?>