There are nearly fifty date and time functions, so for the purpose of this tutorial we will narrow them down to three.
Function | Description |
---|---|
time() | Returns the Current Time as a Unix Timestamp |
date() | Formats a Local Time/Date |
strtotime() | Parses an English Textual Date or Time Into a Unix Timestamp |
Since we will be using the word "timestamp" throughout this page, we will begin by explaining what it is. A Unix Timestamp is the number of seconds that have passed since January 1, 1970. Currently, the timestamp is 1734803357, but since it changes every second, the number will increase if you refresh the page. (The timestamp reflects the time set on the server, and not on the client's computer.)
The time() function returns the current timestamp. The timestamp that you see in the previous paragraph is generated by a very small line of code: <?php echo time(); ?>
The time() function can also return a modified timestamp. You can add or subtract any number of seconds to the function in order to return the timestamp of a previous or upcoming date and time.
For example, to return a timestamp for next week, or to return a timestamp from last week, I can add or subtract 7 days by determining how many seconds are involved (7 days * 24 hours per day * 60 minutes in an hour * 60 seconds in an hour = number of second in 7 days).
<?php
$last_week = time() - (7 * 24 * 60 * 60);
$next_week = time() + (7 * 24 * 60 * 60);
$next_month = time() + (30 * 24 * 60 * 60);
echo "Last Week: " . $last_week . "<br />";
echo "Next Week: " . $next_week . "<br />";
echo "Next Month: " . $next_month . "<br />";
?>
The result will be:
Last Week: 1734198557
Next Week: 1735408157
Next Month: 1737395357
The strtotime() function accepts an English datetime description and turns it into a timestamp. It is a simple way to determine "next week" or "last monday" without using the time() function and a bunch of math.
Some examples are:
<?php
echo strtotime("now") . "<br />";
echo strtotime("tomorrow") . "<br />";
echo strtotime("yesterday") . "<br />";
echo strtotime("10 September 2000") . "<br />";
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") . "<br />";
echo strtotime("4pm + 2 Hours") . "<br />";
echo strtotime("now + 2 fortnights") . "<br />";
echo strtotime("last Monday") . "<br />";
echo strtotime("2pm yesterday") . "<br />";
echo strtotime("7am 12 days ago") . "<br />";
?>
The date() function formats a timestamp so that it actually makes sense, such as 11:49 AM Saturday, December 21, 2024.
The date() function accepts two arguments, according to the following syntax: date(format, timestamp);. The first argument is the format that you want the timestamp in. The second argument (optional) is the timestamp that you want formatted. If no timestamp is supplied, the current timestamp will be used.
PHP provides over thirty-five case-sensitive characters that are used to format the date and time. These characters are:
Character | Description | Example | |
---|---|---|---|
Day | j | Day of the Month, No Leading Zeros | 1 - 31 |
Day | d | Day of the Month, 2 Digits, Leading Zeros | 01 - 31 |
Day | D | Day of the Week, First 3 Letters | Mon - Sun |
Day | l (lowercase 'L') | Day of the Week | Sunday - Saturday |
Day | N | Numeric Day of the Week | 1 (Monday) - 7 (Sunday) |
Day | w | Numeric Day of the Week | 0 (Sunday) - 6 (Saturday) |
Day | S | English Suffix For Day of the Month | st, nd, rd or th |
Day | z | Day of the Year | 0 - 365 |
Week | W | Numeric Week of the Year (Weeks Start on Mon.) | 1 - 52 |
Month | M | Textual Representation of a Month, Three Letters | Jan - Dec |
Month | F | Full Textual Representation of a Month | January - December |
Month | m | Numeric Month, With Leading Zeros | 01 - 12 |
Month | n | Numeric Month, Without Leading Zeros | 1 - 12 |
Month | t | Number of Days in the Given Month | 28 - 31 |
Year | L | Whether It's a Leap Year | Leap Year: 1, Otherwise: 0 |
Year | Y | Numeric Representation of a Year, 4 Digits | 1999, 2003, etc. |
Year | y | 2 Digit Representation of a Year | 99, 03, etc. |
Time | a | Lowercase Ante Meridiem & Post Meridiem | am or pm |
Time | A | Uppercase Ante Meridiem & Post Meridiem | AM or PM |
Time | B | Swatch Internet Time | 000 - 999 |
Time | g | 12-Hour Format Without Leading Zeros | 1 - 12 |
Time | G | 24-Hour Format Without Leading Zeros | 0 - 23 |
Time | h | 12-Hour Format With Leading Zeros | 01 - 12 |
Time | H | 24-Hour Format With Leading Zeros | 00 - 23 |
Time | i | Minutes With Leading Zeros | 00 - 59 |
Time | s | Seconds With Leading Zeros | 00 - 59 |
Timezone | e | Timezone Identifier | Example: UTC, Atlantic |
Timezone | I (capital i) | Whether Date Is In Daylight Saving Time | 1 if DST, otherwise 0 |
Timezone | O | Difference to Greenwich Time In Hours | Example: +0200 |
Timezone | P | Difference to Greenwich Time, With Colon | Example: +02:00 |
Timezone | T | Timezone Abbreviation | Examples: EST, MDT ... |
Timezone | Z | Timezone Offset In Seconds | -43200 through 50400 |
Using a combination of these characters and commas, periods, dashes, semicolons and backslashes, you can now format dates and times.
<?php
// Will Echo: 11:49 AM Saturday, December 21, 2024
echo date("g:i A l, F d, Y");
// Will Echo: 2024-12-20
$yesterday = strtotime("yesterday");
echo date("Y-m-d", $yesterday);
?>
It is very simple to compare two dates in php, to verify if one is past, present or future.
Since all dates can be converted back into timestamps, all it takes is a simple comparison of the two timestamps, which are essentially numbers. If the date in question is a bigger number than the current date, than you know that your date is in the future, and if the date is smaller than the current date, you know that the date is already past.
<?php
$my_date = strtotime("10 April 2005");
if(strtotime($my_date) > time()) {
echo "Future Date!";
} if(strtotime($my_date) < time()) {
echo "Past Date!";
} if(strtotime($my_date) == time()) {
echo "Current Date!";
}
?>