PHP - Date and Time
The PHP date() function is used to format a date and/or a time.
PHP date() function
The PHP date() function returns a string formatted according to the specified format string using the given integer timestamp or the current time if no timestamp is given.
Syntax
date(format, timestamp)
Parameters
format |
Required. Specify the format string to format the outputted date string. Refer to the table below for formatting options. |
timestamp |
Optional. Specify a Unix timestamp representing the date. If it is omitted or null, it defaults to the current local time. |
format parameter string
Format character | Description | Example returned values |
---|---|---|
Day | ||
d | Day of the month, 2 digits with leading zeros | 01 to 31 |
D | A textual representation of a day, three letters | Mon through Sun |
j | Day of the month without leading zeros | 1 to 31 |
l (lowercase 'L') | A full textual representation of the day of the week | Sunday through Saturday |
N | ISO-8601 numeric representation of the day of the week | 1 (for Monday) through 7 (for Sunday) |
S | English ordinal suffix for the day of the month, 2 characters | st, nd, rd or th. Works well with j |
w | Numeric representation of the day of the week | 0 (for Sunday) through 6 (for Saturday) |
z | The day of the year (starting from 0) | 0 through 365 |
Week | ||
W | ISO-8601 week number of year, weeks starting on Monday | Example: 42 (the 42nd week in the year) |
Month | ||
F | A full textual representation of a month, such as January or March | January through December |
m | Numeric representation of a month, with leading zeros | 01 through 12 |
M | A short textual representation of a month, three letters | Jan through Dec |
n | Numeric representation of a month, without leading zeros | 1 through 12 |
t | Number of days in the given month | 28 through 31 |
Year | ||
L | Whether it's a leap year | 1 if it is a leap year, 0 otherwise. |
o | ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. | Examples: 1999 or 2003 |
Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
y | A two digit representation of a year | Examples: 99 or 03 |
Time | ||
a | Lowercase Ante meridiem and Post meridiem | am or pm |
A | Uppercase Ante meridiem and Post meridiem | AM or PM |
B | Swatch Internet time | 000 through 999 |
g | 12-hour format of an hour without leading zeros | 1 through 12 |
G | 24-hour format of an hour without leading zeros | 0 through 23 |
h | 12-hour format of an hour with leading zeros | 01 through 12 |
H | 24-hour format of an hour with leading zeros | 00 through 23 |
i | Minutes with leading zeros | 00 to 59 |
s | Seconds with leading zeros | 00 through 59 |
u | Microseconds. | Example: 654321 |
v | Milliseconds. Same note applies as for u. | Example: 654 |
Timezone | ||
e | Timezone identifier | Examples: UTC, GMT, Atlantic/Azores |
I (capital i) | Whether or not the date is in daylight saving time | 1 if Daylight Saving Time, 0 otherwise. |
O | Difference to Greenwich time (GMT) without colon between hours and minutes | Example: +0200 |
P | Difference to Greenwich time (GMT) with colon between hours and minutes | Example: +02:00 |
p | The same as P, but returns Z instead of +00:00 | Example: +02:00 |
T | Timezone abbreviation, if known; otherwise the GMT offset. | Examples: EST, MDT, +05 |
Z | Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. | -43200 through 50400 |
Full Date/Time | ||
c | ISO 8601 date | 2004-02-12T15:19:21+00:00 |
r | RFC 2822 formatted date | Example: Thu, 21 Dec 2000 16:01:07 +0200 |
U | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | See time() method. |
Get a Date
The example below shows how to use the date() function to get a date.
<?php //display info about current date echo "Today is ". date("Y/m/d") ."\n"; echo "Today is ". date("d-M-Y") ."\n"; echo "Today is ". date("l") ."\n\n"; //display info about given timestamp echo "Timestamp is ". date("Y/m/d", 1500292845) ."\n"; echo "Timestamp is ". date("d-M-Y", 1500292845) ."\n"; ?>
The output of the above code will be similar to:
Today is 2021/11/29 Today is 29-Nov-2021 Today is Monday Timestamp is 2017/07/17 Timestamp is 17-Jul-2017
Get a Time
Consider the example below where the date() function is used to get a time.
<?php //display info about current date echo "Current time is ". date("h:i:s A") ."\n"; //display info about given timestamp echo "Time in Timestamp is ". date("h:i:s A", 1500292845) ."\n"; ?>
The output of the above code will be similar to:
Current time is 04:25:48 AM Time in Timestamp is 12:00:45 PM
Get Date and Time
Consider the example below where the date() function is used to get date and time.
<?php //display info about current date echo "Today: ". date("l, d-M-Y h:i:s A") ."\n"; //display info about given timestamp echo "Timestamp: ". date("l, d-M-Y h:i:s A", 1500292845) ."\n"; ?>
The output of the above code will be similar to:
Today: Monday, 29-Nov-2021 04:27:54 AM Timestamp: Monday, 17-Jul-2017 12:00:45 PM
PHP time() function
The PHP time() function returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Syntax
time()
The example below shows the usage of time() function.
<?php //getting current time measured in the number //of seconds since the Unix Epoch $currtime = time(); echo "Time elapsed since Unix Epoch: ". $currtime." seconds\n"; //using date() function to format the //current time; the date() function uses //time() as second parameter by default echo "Current time: ".date('Y-m-d') ."\n"; ?>
The output of the above code will be similar to:
Time elapsed since Unix Epoch: 1638160528 seconds Current time: 2021-11-29
Create a Date With mktime()
The PHP mktime() function returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Arguments may be left out in order from right to left. Any arguments thus omitted will be set to the current value according to the local date and time.
Syntax
mktime(hour, minute, second, month, day, year)
Parameters
hour |
Optional. Specify the number of the hour relative to the start of the day. Negative values reference the hour before midnight of the day. Values greater than 23 reference the appropriate hour in the following day(s).
Note: This parameter is no longer optional as of PHP 8.0.0. |
minute |
Optional. Specify the number of the minute relative to the start of the hour. Negative values reference the minute in the previous hour. Values greater than 59 reference the appropriate minute in the following hour(s). |
second |
Optional. Specify the number of seconds relative to the start of the minute. Negative values reference the second in the previous minute. Values greater than 59 reference the appropriate second in the following minute(s). |
month |
Optional. Specify the number of the month relative to the end of the previous year. Values less than 1 reference the months in the previous year in reverse order, for example: 0 is December, -1 is November, etc. Values greater than 12 reference the appropriate month in the following year(s). |
day |
Optional. Specify the number of the day relative to the end of the previous month. Values less than 1 reference the days in the previous month, for example: 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s). |
year |
Optional. Specify the number of the year, may be a two or four digit value. If it is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. |
The example below shows the usage of mktime() function.
<?php //creating timestamp using mktime() function $d = mktime(10, 20, 30, 8, 17, 2017); //display info about given timestamp echo "Created Date is: ". date("l, d-M-Y h:i:s A", $d) ."\n"; ?>
The output of the above code will be:
Created Date is: Thursday, 17-Aug-2017 10:20:30 AM
Create a Date from a string with strtotime()
The PHP strtotime() function converts any English textual datetime description into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC).
The function attempts to parse the datetime string into a Unix timestamp, relative to the timestamp given in baseTimestamp, or the current time if baseTimestamp is not provided.
Syntax
strtotime(datetime, baseTimestamp)
Parameters
datetime |
Required. Specify a date/time string. It should be in valid Date and Time Formats. |
baseTimestamp |
Optional. Specify the timestamp which is used as a base for the calculation of relative dates. |
The example below shows the usage of strtotime() function.
<?php //current time as Unix timestamp and //formatting it using date() function echo strtotime("now")."\n"; echo date("d-M-Y", strtotime("now"))."\n"; //1 week ahead of current time echo "\n".strtotime("+1 week")."\n"; echo date("d-M-Y", strtotime("+1 week"))."\n"; //1 week 2 days 5 hours ahead of current time echo "\n".strtotime("+1 week 2 days 5 hours")."\n"; echo date("d-M-Y", strtotime("+1 week 2 days 5 hours"))."\n"; ?>
The output of the above code will be similar to:
1638161505 29-Nov-2021 1638766305 06-Dec-2021 1638957105 08-Dec-2021
Complete PHP Date and Time Reference
For a complete reference of all PHP Date and Time functions, see the complete PHP Data and Time Reference.