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 the 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.
There will be three major motives to use date and time-related functions as listed below.
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 forms 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.
Instead of using getdate() and process over the array it returns to format date, we can retrieved formatted date string by using the 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 the date(‘m-d-Y H: datetimei:s), tDateTimestring will be returned.
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);
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.