PHP Tutorial PHP Advanced PHP References

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 characterDescriptionExample returned values
Day
dDay of the month, 2 digits with leading zeros01 to 31
DA textual representation of a day, three lettersMon through Sun
jDay of the month without leading zeros1 to 31
l (lowercase 'L')A full textual representation of the day of the weekSunday through Saturday
NISO-8601 numeric representation of the day of the week1 (for Monday) through 7 (for Sunday)
SEnglish ordinal suffix for the day of the month, 2 charactersst, nd, rd or th. Works well with j
wNumeric representation of the day of the week0 (for Sunday) through 6 (for Saturday)
zThe day of the year (starting from 0)0 through 365
Week
WISO-8601 week number of year, weeks starting on MondayExample: 42 (the 42nd week in the year)
Month
FA full textual representation of a month, such as January or MarchJanuary through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, three lettersJan through Dec
nNumeric representation of a month, without leading zeros1 through 12
tNumber of days in the given month28 through 31
Year
LWhether it's a leap year1 if it is a leap year, 0 otherwise.
oISO-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
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a yearExamples: 99 or 03
Time
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
BSwatch Internet time000 through 999
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds with leading zeros00 through 59
uMicroseconds.Example: 654321
vMilliseconds. Same note applies as for u.Example: 654
Timezone
eTimezone identifierExamples: UTC, GMT, Atlantic/Azores
I (capital i)Whether or not the date is in daylight saving time1 if Daylight Saving Time, 0 otherwise.
ODifference to Greenwich time (GMT) without colon between hours and minutesExample: +0200
PDifference to Greenwich time (GMT) with colon between hours and minutesExample: +02:00
pThe same as P, but returns Z instead of +00:00Example: +02:00
TTimezone abbreviation, if known; otherwise the GMT offset.Examples: EST, MDT, +05
ZTimezone 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
cISO 8601 date2004-02-12T15:19:21+00:00
rRFC 2822 formatted dateExample: Thu, 21 Dec 2000 16:01:07 +0200
USeconds 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.


5