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 : 1532304936
$local = localtime() :
seconds : $local[0] : 36
minutes : $local[1] : 15
hours (24hr) : $local[2] : 1
day: $local[3] : 23
month (0-11) : $local[4] : 6 (+1 for usual format)
year (+1900) : $local[5] : 118 (+1900 for usual format)
day of week (0-6) : $local[6] : 1
day of year (0-364) : $local[7] : 203
timezone (+/-): $local[8] : 1
getdate($timestamp) :
seconds : $local["seconds"] : 36
minutes : $local["minutes"] : 15
hours : $local["hours"] : 1
mday : $local["mday"] : 23
wday : $local["wday"] : 1
mon : $local["mon"] : 7
year : $local["year"] : 2018
yday : $local["yday"] : 203
weekday : $local["weekday"] : Monday
month : $local["month"] : July
timestamp : $local[0] : 1532304936

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) : Mon

%A - full weekday name according to the current locale

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

%b - abbreviated month name according to the current locale

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

%B - full month name according to the current locale

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

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

strftime("%c",$timestamp) : Mon Jul 23 01:15:36 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) : 23

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

strftime("%D",$timestamp) : 07/23/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) : 23

%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) : Jul

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

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

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

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

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

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

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

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

%M - minute as a decimal number

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

%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) : AM

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

strftime("%r",$timestamp) : 01:15:36 AM

%R - time in 24 hour notation

strftime("%R",$timestamp) : 01:15

%S - second as a decimal number

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

%t - tab character

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

strftime("%T",$timestamp) : 01:15:36

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

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

%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) : 29

%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) : 30

%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) : 30

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

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

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

strftime("%x",$timestamp) : 07/23/18

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

strftime("%X",$timestamp) : 01:15:36

%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:

Mon Monday 07/23/18 00:15:36 AM Jul July Mon Jul 23 00:15:36 2018
Mon Monday 07/23/18 01:15:36 AM Jul July Mon Jul 23 01:15:36 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:

am AM

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") : 23 23 23rd Mon Monday 1

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

Jul July 7 07 31

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

1 01 1 01 15 36

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

2018 18 203

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():

July 23rd 2018 1:15:36am  BST
July 23rd 2018 12:15:36am 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.