Working with dates in PHP
PHP offers a plethora of different date functionalities that make working with dates much easier.
In the most primitive manner, a date is an
represents a timestampTimestampA numeric representation of a millisecond in time.
In PHP a date is exactly this, but to form a date that means something to a human, it is first converted to a string.
A timezone in PHP represents a timezone in the world. For instance, UTC or Coordinated Universal Time is a timezone in the
real world. In PHP the
timezone is also a timezone parameter. Another timezone is
Any timezone can be used to represent a real world timezone and all subsequent date representations will be in that timezone, so conversion between different timezones is much easier.
If a timezone is not specified, PHP will still compile but it may throw a warning on the page. It is advised that a timezone is specified.
Now all future dates will be in their 'Europe/London' format.
There are too many different timezones to place in this article, but they can all be found in the PHP manual.
Working with dates
As mentioned before, date in PHP are converted from a timestamp to a string type in order
for them to display properly. PHP makes this easy with the
date function can also be used to represent times. To declare a date in a proper date format, the
function is used as follows:
date ("Day Month Year Hour Minute Second", timestamp);
Every parameter of the string is entirely optional for instance, an hour value may be the only one specified to obtain the hour. The timestamp parameter is also optional.
Specifying a timestamp parameter to another date (for instance using the
time() function will return the current time) will turn that
time into a date of that format. As mentioned before, this is optional and if it is not specified the default value will be the current time (which is the equivelant
to calling the
date ("dS F Y g:i:s A");
mktime() function can also be used to generate a time as well which can in turn be passed in to the
mktime function is used as follows:
//Creates a time to the 12th of July 2014 at 13:34:29. $time1 = mktime (13, 34, 29, 7, 12, 2014); //Creates a time to the 26th of January 2013 at 9:12:43. $time2 = mktime (9, 12, 43, 1, 26, 2013);
The following parameters are for the day and week parameters of the
|d||Numeric day of month with leading zeros||09, 13, 16|
|D||Three letter representation of day of week||Mon, Fri, Sun|
|j||Numeric day of the month without leading zeros||1, 7, 13|
|l||Full name of day of week||Monday, Friday, Sunday|
|S||Day of month suffix||st, nd, rd, th|
|w||Numeric representation of the day of the week (0 to 6)||1, 2, 6|
|z||Day of year (0 - 365)||41, 53, 213|
|W||Numeric representation of the week of the year following ISO-8601 (1 to 52)||1, 2, 6|
The following parameters are for the month value of the date:
|F||Full name of a month||January, July, August, December|
|M||Three letter representation a month||Jan, Jul, Aug, Dec|
|m||Numeric representation of the month with leading zeros(01-12)||01, 04, 12|
|n||Numeric representation of the month without the leading zeros (1-12)||1, 4, 12|
|t||Number of days in a month||28, 29, 30, 31|
The next parameters affect the year parameter of the date:
|L||States whether the year is a leap year or not||0 (if it is not a leap year) or 1 (if it is a leap year)|
|Y||Full numeric representation of the year in four digits||1956, 1957, 1991, 1993, 2015|
|y||Full numeric representation of the year in four digits||56, 57, 91, 93, 15|
As mentioned before, times can be represented using the
|a||Lowercase Ante meridiem and Post meridiem||am or pm|
|A||Uppercase Ante meridiem and Post meridiem||AM or PM|
|g||12 hour format hour without leading zeros||1, 3, 11|
|G||24 hour format hour without leading zeros||1, 3, 11, 15, 18, 21|
|h||12 hour format hour with leading zeros||01, 03, 11|
|H||24 hour format hour without leading zeros||01, 03, 11, 15, 18, 21|
|i||Minutes with leading zeros||00, 05, 14, 42, 59|
|s||Seconds with leading zeros||00, 05, 14, 42, 59|