After a little break, happy to meet PHP readers again. Now, we are going to see PHP functions that are used to get the current date. There are several PHP-supported date functions in this regard but varied based upon the arguments, return type and etc.
These functions are listed as follows that will be discussed in detail in upcoming paragraphs.
We have seen the first two of the above list while seeing about PHP date functions. Let us see these functions with some more details with examples.
This PHP function expects at least one parameter to represent the date format to be returned. And, it includes an optional second argument to specify the timestamp of a particular date.
Since we are seeing about the possible ways of getting the current date, this second argument is not required for us. If needed, we can pass timestamp by using time(), mktime() functions.
For the first parameter, we can use either the combination of available PHP date format characters, for example, m,d, or y for returning two-digit month, day and year values, respectively. Or otherwise, we can use the PHP date constants, like, DATE_W3C, DATE_ISO8601 and etc.
The following PHP code deals with this date(), by using the above two ways of providing date format parameter.
<?php print "<br/><b>PHP date() with date format characters returns.</b><br/>"; print date('d-m-y'); print "<br/><br/><b>PHP date() with date constants returns.</b><br/>"; print date(DATE_ISO8601); ?>
This program will return the following output to the browser.
PHP date() with date format characters returns. 16-08-13 PHP date() with date constants returns. 2013-08-16T04:55:16+0200
On successful execution of this PHP function, it returns the formatted date string as specified. If anything is wrong with the specified parameters, like an invalid date format character or timezone, then a PHP error message will be returned to the browser.
This function will return an array of date components, like, mday, wday, month, year, hour and etc. We need to group these components into a required format after getting the resultant array. Using this function, formatting dates in PHP is a little bit longer compared with some direct functions.
Now, let us have a simple PHP example to get current date components as follows.
<?php print "<br/><br/><b>PHP getdate() returns current date components.</b><br/>"; $current_date_components = getdate(); print "<PRE>"; print_r($current_date_components); print "</PRE>"; ?>
The array of date components that are returned by this program is,
PHP getdate() returns current date components. Array ( [seconds] => 19 [minutes] => 9 [hours] => 6 [mday] => 16 [wday] => 5 [mon] => 8 [year] => 2013 [yday] => 227 [weekday] => Friday [month] => August  => 1376626159 )
This date function is very similar to the PHP date() function on getting the date or time or both with the specified format. But, the only difference is, that it will return the date time formatted string with respect to Greenwich Mean Time (GMT).
Other than that, the format string parameter and optional timestamp parameter are passed to this function also, as we have seen for the date(). So replace date() with gmdate(), in the above program, and the code is,
<?php print "<br/><b>PHP gmdate() with date format characters returns.</b><br/>"; print gmdate('d-m-y H:i:s'); print "<br/><br/><b>PHP gmdate() with date constants returns.</b><br/>"; print gmdate(DATE_ISO8601); ?>
We have included time format characters for the above program to see the difference in the output return by gmdate(). And, the output is,
PHP gmdate() with date format characters returns. 16-08-13 04:00:19 PHP gmdate() with date constants returns. 2013-08-16T04:00:19+0000
Another similar PHP date function, as like as date() is, idate(). This function accepts any one of the date format characters as its first parameter, whereas, in date(), a combination of these characters are specified.
And, another difference between date() and idate() is, that the latter will return an integer value corresponding date format charter specified, whereas date() returns the string instead.
<?php print "<br/><br/><b>PHP getdate() returns current date components.</b><br/>"; $current_date_components = idate("Y"); print "<PRE>"; print_r($current_date_components); print "</PRE>"; ?>
This program will return integer values for the day, month and year of the current date on each idate() invoke with respective date format characters. And, the above program will display the following output.
PHP idate("d") returns integer represents day. 16 PHP idate("m") returns integer represents month. 8 PHP idate("Y") returns four digit integer represents year. 2013