Writing PHP - 10: PHP Date and Time functions

Current time, date calculations, timestamp formats.

Date and time functions

Unlike Javascript, note that when PHP retrieves the time, it will be the local server time, not necessarily your local (browser) time. You will be able to tell if this page is cached if you refresh the page and the values do not change. Time is usually retrieved as a timestamp - an integer value - and then processed into whichever values you require:

$timestamp = time() : $timestamp : 1524433661
$local = localtime() :
seconds : $local[0] : 41
minutes : $local[1] : 47
hours (24hr) : $local[2] : 22
day: $local[3] : 22
month (0-11) : $local[4] : 3 (+1 for usual format)
year (+1900) : $local[5] : 118 (+1900 for usual format)
day of week (0-6) : $local[6] : 0
day of year (0-364) : $local[7] : 111
timezone (+/-): $local[8] : 1
getdate($timestamp) :
seconds : $local["seconds"] : 41
minutes : $local["minutes"] : 47
hours : $local["hours"] : 22
mday : $local["mday"] : 22
wday : $local["wday"] : 0
mon : $local["mon"] : 4
year : $local["year"] : 2018
yday : $local["yday"] : 111
weekday : $local["weekday"] : Sunday
month : $local["month"] : April
timestamp : $local[0] : 1524433661

Alternatively, you can convert any timestamp (past,present or future) directly into a formatted string.
Use either strftime() or date():
%a - abbreviated weekday name according to the current locale :

strftime("%D",$timestamp) : Sun

%A - full weekday name according to the current locale

strftime("%A",$timestamp) : Sunday

%b - abbreviated month name according to the current locale

strftime("%b",$timestamp) : Apr

%B - full month name according to the current locale

strftime("%B",$timestamp) : April

%c - preferred date and time representation for the current locale

strftime("%c",$timestamp) : Sun Apr 22 22:47:41 2018

%C - century number (the year divided by 100 and truncated to an integer, range 00 to 99).

strftime("%C",$timestamp) : 20

Adding an ordinal to the century number

Remember to +1. To add the correct ordinal suffix, you need to play around with the values to mktime() - creating a false, temporary, timestamp to set the day to the year+1 - then date("S",$falsetimestamp) can create the correct ordinal. The $hour, $minute, $second, $month and $year values for mktime() can be completely arbitrary for the purposes of this conversion.

$temp = strftime("%C",$timestamp);
$temp++;
$a = $temp;
$temp = mktime($hour,$minute,$second,$month,$temp,$year);
$a .= date("S",$temp) . " century";
echo $a;

21st century

%d - day of the month as a decimal number (range 01 to 31)

strftime("%d",$timestamp) : 22

%D - same as %m/%d/%y

strftime("%D",$timestamp) : 04/22/18

%e - day of the month as a decimal number, a single digit is preceded by a space (range ' 1' to '31')

strftime("%e",$timestamp) : 22

%g - like %G, but without the century.

strftime("%g",$timestamp) : 18

%G - The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead.

strftime("%G",$timestamp) : 2018

%h - same as %b

strftime("%h",$timestamp) : Apr

%H - hour as a decimal number using a 24-hour clock (range 00 to 23)

strftime("%H",$timestamp) : 22

%I - hour as a decimal number using a 12-hour clock (range 01 to 12)

strftime("%I",$timestamp) : 10

%j - day of the year as a decimal number (range 001 to 366)

strftime("%j",$timestamp) : 112

%m - month as a decimal number (range 01 to 12)

strftime("%m",$timestamp) : 04

%M - minute as a decimal number

strftime("%M",$timestamp) : 47

%n - newline character

%p - either 'am' or 'pm' according to the given time value, or the corresponding strings for the current locale

strftime("%p",$timestamp) : PM

%r - time in a.m. and p.m. notation

strftime("%r",$timestamp) : 10:47:41 PM

%R - time in 24 hour notation

strftime("%R",$timestamp) : 22:47

%S - second as a decimal number

strftime("%S",$timestamp) : 41

%t - tab character

%T - current time, equal to %H:%M:%S

strftime("%T",$timestamp) : 22:47:41

%u - weekday as a decimal number [1,7], with 1 representing Monday. Sun Solaris seems to start with Sunday as 1.

strftime("%u",$timestamp) : 7

%U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week

strftime("%U",$timestamp) : 16

%V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week. (Use %G or %g for the year component that corresponds to the week number for the specified timestamp.)

strftime("%V",$timestamp) : 16

%W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week.

strftime("%W",$timestamp) : 16

%w - day of the week as a decimal, Sunday being 0.

strftime("%w",$timestamp) : 0

%x - preferred date representation for the current locale without the time.

strftime("%x",$timestamp) : 04/22/18

%X - preferred time representation for the current locale without the date.

strftime("%X",$timestamp) : 22:47:41

%y - year as a decimal number without a century (range 00 to 99).

strftime("%y",$timestamp) : 18

%Y - year as a decimal number including the century.

strftime("%Y",$timestamp) : 2018

%Z - time zone or name or abbreviation.

strftime("%Z",$timestamp) : BST

Converting strftime() to GMT with gmstrftime()

For example: gmstrftime("%a %A %x %X %p %b %B %c"); compared with the strftime() version:

Sun Sunday 04/22/18 21:47:41 PM Apr April Sun Apr 22 21:47:41 2018
Sun Sunday 04/22/18 22:47:41 PM Apr April Sun Apr 22 22:47:41 2018

date() and gmdate() use different formatting instructions to strftime() gmstrftime()!

date() and gmdate() convert the localtime directly to a string, with or without a separate timestamp - for example: date("a",$timestamp).

date("a") date("A"): am | pm or AM | PM:

pm PM

Days: day of month as digit with leading zeroes - d, without zeroes - j, repeat, this time with an ordinal suffix - jS, 3 letter text - D, or long - l (lowercase L), numerical day of the week (0=Sunday) - w.

date("d j jS D l w") : 22 22 22nd Sun Sunday 0

Months: Short name - M, long name - F, number - n, with zeros - m, total number of days in this month - t

Apr April 4 04 30

Hours and minutes : 12hour format - g, with zeroes - h, 24hr format - G, with zeroes - H, minutes - i, seconds - s

10 10 22 22 47 41

Years: 4 digits - Y, 2 digits - y, current day of the year (0-365) - z

2018 18 111

Timezones: server timezone - T, timezone offset in seconds (+/-) - Z. Divide by 3600 for hours.

BST 3600 /3600 = 1hour(s)

A complete timestring using date() and then gmdate():

April 22nd 2018 10:47:41pm  BST
April 22nd 2018 9:47:41pm GMT

date("F jS Y g:i:sa T")


This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.