PHP Dates and Times

Dates and times can be important elements in a web database application. PHP has the capability to recognize dates and times and handle them differently than plain character strings. Dates and times are stored by the computer in a format called a timestamp. Three important functions are date(), time() and strtotime().

images/articles/php/php-dates-and-times.jpg

PHP converts dates from your notation into a timestamp that the computer understands and from a timestamp into a format familiar to people. PHP handles dates and times with built-in functions.

1. Unix Timestamp

The timestamp format is a Unix Timestamp, which is an integer that is the number of seconds from January 1, 1970, 00:00:00 GMT (Greenwich Mean Time) to the time represented by the timestamp. This format makes it easy to calculate the time between two dates, You just need to subtract one timestamp from the other.

2. Validating Dates

The checkdate() function validates dates quite, returning TRUE if the supplied date is valid and FALSE otherwise. 

3. Formatting Date

The function that you will use most often is date(), which converts a date or time from the timestamp format into a format that you specify. The general format is

$mydate = date('format', $timestamp);

If $timestamp isn't included, the current time is obtained from the operating system and used. Thus, you can get today’s date with the following:

$today = date('Y/m/d');

The format is a string that specifies the date format that you want stored in the variable. The parts of the date can be separated by a hyphen (-), a dot (.), a forward slash (/), or a space.

Month

  • F: Month in text, not abbreviated (January)
  • M: Month in text, abbreviated (Jan)
  • m: Month in numbers with leading zeros (02, 12)
  • n: Month in numbers without leading zeros (1, 12)

Day

  • d: Day of the month; two digits with leading zeros (01, 14)
  • j: Day of the month without leading zeros (3, 30)
  • l: Day of the week in text, not abbreviated (Friday)
  • D: Day of the week in text, abbreviated (Fri)
  • w: Day of the week in numbers From 0 (Sunday) to 6 (Saturday)

Year

  • Y: Year in four digits (2015)
  • y: Year in two digits (02)

Hour

  • g: Hour between 0 and 12 without leading zeros (2, 10)
  • G: Hour between 0 and 24 without leading zeros (2, 15)
  • h: Hour between 0 and 12 with leading zeros (01, 10)
  • H: Hour between 0 and 24 with leading zeros (00, 23)

Minutes and Seconds

  • i: Minutes (00, 59)
  • s: Seconds (00, 59)
  • a: am or pm in lowercase (am, pm)
  • A: AM or PM in uppercase (AM, PM)

Working with Timestamps

PHP offers two functions for working with timestamps: time() and mktime(). The former is useful for retrieving the current timestamp, whereas the latter is useful for retrieving a timestamp corresponding to a specific date and time.

You can assign a timestamp with the current date and time to a variable with the following statement:

$today = time();

Another way to store a current timestamp is with the statement:

$today = strtotime('today');

You can store specific timestamps by using strtotime with various keywords and abbreviations that are similar to English. For example,

$importantDate = strtotime('January 15 2018');

The strtotime statement recognizes the following words and abbreviations:

  • Month names: Twelve month names and abbreviations
  • Days of the week: Seven days and some abbreviations
  • Time units: year, month, fortnight, week, day, hour, minute, second, am, pm
  • Some useful English words: ago, now, last, next, this, tomorrow, yesterday
  • Plus and minus: + or -
  • All numbers
  • Time zones: For example, gmt (Greenwich Mean Time), pdt (Pacific Daylight Time), and akst (Alaska Standard Time)

Converting Timestamp to User-Friendly Values

The getdate() function accepts a timestamp and returns an associative array consisting of its components. In total, 11 array elements are returned:

  1. hours: Numeric representation of the hours. The range is 0 through 23.
  2. mday: Numeric representation of the day of the month. The range is 1 through 31.
  3. minutes: Numeric representation of the minutes. The range is 0 through 59.
  4. mon: Numeric representation of the month. The range is 1 through 12.
  5. month: Complete text representation of the month, e.g., July.
  6. seconds: Numeric representation of the seconds. The range is 0 through 59.
  7. wday: Numeric representation of the day of the week, e.g., 0 for Sunday.
  8. weekday: Complete text representation of the day of the week, e.g., Friday.
  9. yday: Numeric offset of the day of the year. The range is 0 through 364.
  10. year: Four-digit numeric representation of the year, e.g., 2010.
  11. 0: Number of seconds since the Unix epoch (timestamp).