learn

Date and Time

category: Advanced
Created by: Dr.Ferrous

Date and Time

category: Advanced
Created by: Dr.Ferrous
twitter google+ facebook pinned 

PHP has multiple functions to get and format date and time. Some of the most used are:

  • date() - formats a timestamp to a more readable date and time
  • time() - Return current Unix timestamp (the number of seconds since January 1 1970)
  • microtime() Return current Unix timestamp with microseconds
  • getdate() - Returns an associative array containing the date/time information

These functions allow you to get the date and time from the server where your PHP scripts are running, they dependent on the locale settings of your server.

PHP date()

The PHP date() function is used to format a timestamp date/time.
Syntax:

  • format - A string format of the outputted date. Ex:   "D, d M Y H:i:s" => "Mon, 15 Aug 2012 15:42:01 +0000"
  • timestamp - An integer Unix timestamp. It's optional. If not added, defaults to the current local time.
    The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

This function returns a formatted date string, or False on error.

 

Example:

 

By default, the date() function use the local server timezone, to set another default timezone, use the date_default_timezone_set('timezone_identifier'), prior to calling any of the date and time functions.
The "timezone_identifier" is a string with the timezone identifier (i.g 'America/New_York' or 'Europe/Lisbon').

The complete list of timezones supported by PHP is available in the List of Supported Timezones.


Example:

if you don't specify the timestamp, the date() function will use the current date and time.
To get the current timestamp, use the time() function (i.g $ts = time(); ).

Other examples with functions for Date and Time

If you want to get and use the Unix timestamp for a date, you can use the mktime() function.
Syntax:

Returns the Unix timestamp corresponding the arguments given.

 

Also, you can use the strtotime() function. Parse an English textual datetime description into a Unix timestamp.
Syntax:

 

The "time" parameter is a date/time string, in english literary form (i.g   "now", "+1 Day", "15 October 2012", "next Sunday", etc.). A list of valid formats is available in the Date and Time Formats.

Example with mktime() and strtotime():

The 0 day of a month represents the last day of the previous month. For example, 0 day of March 2012 is "29" the last day of February 2012

 

Another useful function for date and time is getdate().
Syntax:

  • timestamp - An integer Unix timestamp. It's optional. If not added, defaults to the current local time.


getdate() returns an associative array of information related to the timestamp, or the current local time if no parameter is given.

Example:

 

PHP has lots of functions for working with date and time. A complete list can be found to: Date/Time Functions

- microtime() returns the current Unix timestamp with microseconds. It can be used to find the execution time of a php script.

Example: