Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Writing PHP

By Neil Williams
2007-11-11


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 : 1194481656
$local = localtime() :
seconds : $local[0] : 36
minutes : $local[1] : 27
hours (24hr) : $local[2] : 0
day: $local[3] : 8
month (0-11) : $local[4] : 10 (+1 for usual format)
year (+1900) : $local[5] : 107 (+1900 for usual format)
day of week (0-6) : $local[6] : 4
day of year (0-364) : $local[7] : 311
timezone (+/-): $local[8] : 0
getdate($timestamp) :
seconds : $local["seconds"] : 36
minutes : $local["minutes"] : 27
hours : $local["hours"] : 0
mday : $local["mday"] : 8
wday : $local["wday"] : 4
mon : $local["mon"] : 11
year : $local["year"] : 2007
yday : $local["yday"] : 311
weekday : $local["weekday"] : Thursday
month : $local["month"] : November
timestamp : $local[0] : 1194481656

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

%A - full weekday name according to the current locale

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

%b - abbreviated month name according to the current locale

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

%B - full month name according to the current locale

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

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

strftime("%c",$timestamp) : Thu Nov  8 00:27:36 2007

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

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

strftime("%D",$timestamp) : 11/08/07

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

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

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

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

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

%h - same as %b

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

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

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

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

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

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

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

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

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

%M - minute as a decimal number

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

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

%R - time in 24 hour notation

strftime("%R",$timestamp) : 00:27

%S - second as a decimal number

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

%t - tab character

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

strftime("%T",$timestamp) : 00:27: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) : 4

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

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

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

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

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

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

strftime("%x",$timestamp) : 11/08/07

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

strftime("%X",$timestamp) : 00:27:36

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

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

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

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

%Z - time zone or name or abbreviation.

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

Converting strftime() to GMT with gmstrftime()

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

Thu Thursday 11/08/07 00:27:36 AM Nov November Thu Nov  8 00:27:36 2007
Thu Thursday 11/08/07 00:27:36 AM Nov November Thu Nov 8 00:27:36 2007

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") : 08 8 8th Thu Thursday 4

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

Nov November 11 11 30

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

12 12 0 00 27 36

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

2007 07 311

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

UTC 0 /3600 = 0hour(s)

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

November 8th 2007 12:27:36am  UTC

November 8th 2007 12:27:36am GMT



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


Tutorial Pages:
» What can PHP do?
» PHP output
» PHP and HTTP: headers and variables
» No need for CGI access
» Preventing and solving bugs in PHP
» Checking user input for dubious or erroneous values
» Read remote files, read and write local files on the server
» Common PHP functions
» Building, sorting, reversing and flipping arrays
» Current time, date calculations, timestamp formats
» PHP string functions


Copyright © Neil Williams


 | Bookmark
Related Tutorials:
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources