May 11, 2021 PHP
The PHP date() function is used to format the time/date.
The date() function returns a string that strings the integer timestamp strings in a given format.
The PHP date() function formats the timestamp as a better readable date and time.
A timestamp is a sequence of characters that represents the date/time a certain event occurred.
string date ( string $format [, int $timestamp ] )
parameter | describe |
---|---|
format | Required.Specify the format of the timestamp. |
timestamp | Optional.Specify timestamp.The default is the current date and time. |
The first required parameter of the date() function, format, specifies how the date/time is formatted.
Here are some of the available characters:
d - Represents the day of the month (01 - 31)
m - Representing the month (01 - 12)
Y - Representing year (four digits)
For a list of all the characters available in the format parameter, check out our PHP Date reference manual, date() function.
You can insert other characters between letters, such as "/"," "." or "-", so you can add additional formats:
<?php
echo date("Y/m/d") . "<br>";
echo date("Y.m.d") . "<br>";
echo date("Y-m-d");
?>
The output of the code above looks like this:
2009/05/11
2009.05.11
2009-05-11
The string of formatting words recognizes the
format
the following format parameters:
Format character | Description | Returns an example of a value |
---|---|---|
Day | --- | --- |
D | On the first day of the month, there are 2 digits leading to zero | 01 to 31 |
D | On the first day of the week, the text indicates three letters | Mon to Sun |
J | On the first day of the month, there is no leading zero | 1 to 31 |
l (lowercase letter "L") | Day of the week, full text format | Sunday to Saturday |
N | IsO-8601 format number represents the day of the week (PHP 5.1.0 new addition) | 1 (for Monday) to 7 (for Sunday) |
S | English suffix after days per month, 2 characters | st, nd, rd, or th. Can be used with j |
W | On the first day of the week, the numbers indicate | 0 (for Sunday) to 6 (for Saturday) |
Z | The first day of the year | 0 to 365 |
Week | --- | --- |
W | Week of the year in the ISO-8601 format, starting on Mondays (PHP 4.1.0 added) | Example: 42 (week 42 of the year) |
Month | --- | --- |
F | month, the full text format, such as January or March | January to December |
M | The number represents the month in which there is a leading zero | 01 to 12 |
M | A month represented by three abbreviations | Jan to Dec |
N | Numbers represent months without leading zeros | 1 to 12 |
T | The number of days due in a given month | 28 to 31 |
Years | --- | --- |
L | Whether it is a leap year | If the leap year is 1, otherwise it is 0 |
O | ISO-8601 format year number. T his is the same value as Y, except that the number of weeks (W) of the ISO belongs to the previous year or the following year. (PHP 5.1.0 new addition) | Examples: 1999 or 2003 |
Y | The year that the 4 digits are fully represented | For example: 1999 or 2003 |
Y | The year represented by a 2-digit number | E.g: 99 or 03 |
Time | --- | --- |
A | Small morning and afternoon values | am or pm |
A | Capital morning and afternoon values | AM or PM |
B | Swatch Internet standard | 000 arrive 999 |
G | Hour, 12 hour format, no leading zero | 1 to 12 |
G | Hourly, 24-hour format with no leading zeros | 0 arrive 23 |
H | Hourly, 12-hour format with leading zeros | 01 to 12 |
H | Hourly, 24-hour format with leading zeros | 00 arrive 23 |
Ⅰ | The number of minutes with leading zero | 00 arrive 59 > |
s | Seconds, leading zero | 00 arrive 59 > |
u | milliseconds (PHP 5.2.2 new additions). It is important to note that the date() function always returns 000000 because it only accepts the integer parameter, while DateTime::format() supports milliseconds. | Example: 654321 |
Time | --- | --- |
E | Time Zone Identification (PHP 5.1.0 Added) | For example: UTC, GMT, Atlantic/Azores |
Ⅰ | Whether it is daylight saving time | If daylight saving time is 1, otherwise it is 0 |
O | The number of hours that are different from Greenwich Time | For example: s0200 |
P | Difference from Greenwich Mean Time (GMT), colon separation between hours and minutes (PHP 5.1.3 new additions) | For example: .02:00 |
T | The time zone in which the machine is located | For example: EST, MDT is in full text format under Windows, such as Eastern Standard Time, and the Chinese version shows China Standard Time. |
Z | The number of seconds of the time difference offset. The time zone offset on the west side of UTC is always negative, and the time zone offset on the east side of UTC is always positive. | -43200 to 43200 |
Full date/time | --- | --- |
C | Date in ISO 8601 format (PHP 5 new addition) | 2004-02-12T15:19:21+00:00 |
R | The date of the RFC 822 format | For example: Thu, 21 Dec 2000 16:01:07 |
U | Number of seconds from the Unix era (January 1 1970 00:00:00 GMT). | See time() |
The second optional parameter of the date() function, timestamp, specifies a timestamp. If you do not provide a timestamp, the current date and time will be used.
The mktime() function returns a Unix timestamp for the specified date.
The Unix timestamp contains the number of seconds between the Unix era (1970-01-01 00:00:00 GMT) and the specified time.
mktime(hour,minute,second,month,day,year,is_dst)
To get a timestamp for a particular day, we just set the day parameter of the mktime() function:
<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>
The output of the code above looks like this:
Tomorrow is 2009/05/12
To view the full reference manual for all date functions, please visit our complete PHP Date reference manual.
This reference manual provides a brief description of each function and an example of its application!
In the next section, we'll move on to the PHP date() function.