After a little break, happy to meet PHP readers again. Now, we are going to see PHP functions that are used to get 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 with upcoming paragraphs.
We have seen about 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 current date, this second argument is not required for us. If needed, we can pass timestamp by using time(), mktime() functions.
For first parameter, we can use either the combination of available PHP date format characters, for example, m,d,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 formatted date string as specified. If anything wrong with the specified parameters, like invalid date format character or timezone, then PHP error message will be return to the browser.
This function will return 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 little bit lengthy 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 PHP date() function on getting date or time or both with specified format. But, the only difference is, that it will return date time formated string with respect to Greenwich Mean Time (GMT).
Other than that, the format string parameter and optional timestamp parameter are passed for this function also, as we have seen for 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 date format character as its first parameter, where as, in date(), combination of these characters are specified.
And, another difference between date() and idate() is, the latter will return integer value corresponding date format charter specified, where as date() returns string instead.
<?php print "<br/><br/><b>PHP idate() returns two digit integer for day.</b><br/>"; print idate('d'); print "<br/><br/><b>PHP idate() returns two digit integer for month.</b><br/>"; print idate('m'); print "<br/><br/><b>PHP idate() returns four digit integer for year.</b><br/>"; print idate('Y'); ?>
This program will return integer values for day,month and year of current date on each idate() invoke with respective date format characters. And, the above program will display the following output.
PHP idate() returns two digit integer represents day. 16 PHP idate() returns two digit integer represents month. 8 PHP idate() returns four digit integer represents year. 2013
This PHP code tutorial was published on August 16, 2013.