The PHP date function is a very useful function that allows us to format a date to our liking. It is used to return a string containing a date formatted based on the pattern that we pass to it. It can be used to format the current date and time (if no additional parameter is passed to the function) or it can be used to format some other time if a timestamp is given.
For the PHP date function to work properly, the timezone that PHP uses must be configured correctly. See how to set the timezone that PHP uses for an explanation of configuring the timezone properly.
The timestamps that we are concerned with is the Unix method of storing the date. In the Unix method, it is a system of defining the date based on the number of seconds (excluding leap seconds) that have elapsed since the Unix epoch which is simply 00:00:00 UTC on January 1, 1970.
By passing a timestamp to the date function we are able to format what would otherwise be a really big number into a readable date and time. If no timestamp is passed to the function, PHP will assume that we want to use the current date and time.
The examples below will use the date function without a timestamp parameter. If you want to use the timestamp feature, simply pass the timestamp as the second parameter to the function.
Below is the basic syntax for using the date function.
$date_formatted = date($format); //OR //$timestamp should be an integer $date_formatted = date($format, $timestamp);
The $date_formatted variable will contain a string of the date after it has been formatted. The $format variable contains the information on how to format the date and we will see the available features below.
Various letters of the alphabet are used as tokens to describe to PHP how the date should be formatted. Each letter corresponds to either the day, week, month, year, time, or timezone. There are also different letters to change how each part of the date may be output, for example the month could be "1" or "Jan" etc.
Never mind the formatting options for now (it should be pretty obvious what these ones do), but if we run the following code:
<?php echo date("D - m/d/y"); ?>
We will get the following output. It changes automatically as well :-)
As you can see, where there were tokens in the PHP code, the interpreter went and replaced those with the appropriate part of the actual date.
By using the available "tokens" (as shown in the table below) to represent the pieces of the date that we want, we are able to format the date to our heart's liking.
| Token | Description | Example |
|---|---|---|
| Day | ||
| d | Day of the month (with leading zeros) | 01 to 31 |
| j | Day of the month (without leading zeros) | 1 to 31 |
| D | 3 letter text representation of the day | Mon through Sun |
| l | Full name of the day | Sunday through Saturday |
| S | 2 character suffix for the day of the month |
st, nd, rd or th. |
| w | The number of the day of the week | 0 (for Sunday) through 6 (for Saturday) |
| Month | ||
| F | Full name of the month | January through December |
| m | What month in the year (with leading zeros) | 01 through 12 |
| n | What month in the year (without leading zeros) | 1 through 12 |
| M | 3 letter text representation of a month | Jan through Dec |
| t | Number of days in the month | 28 through 31 |
| Year | ||
| L | Whether year is a leap year | 1 for leap year, 0 otherwise. |
| Y | 4 digit numeric representation of a year | Examples: 1995 or 2009 |
| y | 2 digit numeric representation of a year | Examples: 95 or 09 |
| Time | ||
| a | See example | am or pm |
| A | See example | AM or PM |
| g | 12-hour format of hour (without leading zeros) | 1 through 12 |
| h | 12-hour format of hour (with leading zeros) | 01 through 12 |
| G | 24-hour format of hour (without leading zeros) | 0 through 23 |
| H | 24-hour format of hour (with leading zeros) | 00 through 23 |
| i | Minutes (with leading zeros) | 00 to 59 |
| s | Seconds (with leading zeros) | 00 through 59 |
| Timezone | ||
| O | Difference to Greenwich time (GMT) in hours | Example: +0200 |
| T | Timezone abbreviation | EST, MDT |
| Full Date | ||
| r | RFC 2822 formatted date | Thu, 21 Dec 2000 16:01:07 +0200 |
If we run the following code:
<?php echo date("l - \T\h\e jS \d\a\y \of F"); ?>
We will get the following output:
As always, if the want to output something literally (in this case we want to output words without their letters being used as part of the date formatting) we use the backslash to escape them.