Dates and Time in PHP

It would be very easy and also interesting to work with date and time related functions, since PHP has very useful functions in this regard. Using these functions we can have the required details both for current date or for some specified date which will be sent as arguments to these function. So, the PHP date constructs like date() and time() is provided as overloaded functions which may or may not get date value as its argument.

php_date_time

There will be three major motives to use date and time related functions as listed below.

  1. Getting array of date components
  2. Getting formatted date or datetime string
  3. getting timestamp

Getting array of date components

A date value contains several components like, day, month, year and sometimes include hour, minute and seconds based on the request. And these component have several form to be displayed. For example, the month of the year can either be displayed in number or string. PHP functions will help to grab these date component and its several display format into an array. This will be obtained by getdate() function of PHP. The example of using getdate() is as follows.

$date_components = getdate();

The required components of current date will be stored in $date_components as an array. By iterating over the key, value pairs of this array, then can be displayed or can be used to format date string.

Getting formatted date or datetime string

Instead of using getdate() and process over the array it returns to format date, we can retrieved formatted date string by using date() function. This is widely used built in function to perform date related work in PHP is the date() function. Following example shows how to use this method.

$current_date = date('m-d-Y');
// Will print current date
// in mm-dd-yyyy format 
echo $current_date;

If we use it as date(‘m-d-Y H:i:s), then the datetime string will be returned.

getting timestamp

The time() function is used to get the timestamp of the current date and time. Timestamp is nothing but the number of second passed from January 1st, 1970 midnight at GMT. And then, if we want to get timestamp of a specified date we should use the PHP function mktime(). The argument of this method should be Hour, Minute, Second, Month, Day and Year with same order. The following example will show this below.

// Will return timestamp for current date, time
$current_time = time();
// Will return timestamp for 25th April 2013
$timestamp = mktime(25, 35, 10, 4, 25, 2013);

Other date and time related functions

Apart from the above three functionality, there is some other interesting functions in PHP.

For example, we can validate a date in server side using function named as checkdate(). It received month, day and year as its argument to validate it. The date value pass though this validation is not guaranteed to say that it can be used to work on PHP built in functions. Why because, all PHP date functions will be work for the date after January 1st, 1970, where the checkdate() will validate and return true even if the year value of a given date below 1970.

Though PHP doesn’t have any in-built function for timezone conversion, we can obtain this by using DateTime and DateTimezone class for PHP version 5.2 and later.

This PHP code tutorial was published on April 25, 2013.

↑ Back to Top