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.
- date() – This is the widely used function, returns local date or time.
- getdate() – It returns a list of date components as an array.
- gmdate() – Same as PHP date(), but instead of local date/time, it returns the same with respect to GMT.
- idate() – Returns integer value representing date components.
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.
How to Get the Current Date in PHP
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,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. 12-11-21 PHP date() with date constants returns. 2021-11-12T20:49:40+0000
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 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 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] => 0 [minutes] => 51 [hours] => 20 [mday] => 12 [wday] => 5 [mon] => 11 [year] => 2021 [yday] => 315 [weekday] => Friday [month] => November  => 1636750260 )
This date function is very similar to PHP date() function on getting date or time or both with the specified format. But, the only difference is, that it will return 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. 12-11-21 20:51:59 PHP gmdate() with date constants returns. 2021-11-12T20:51:59+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, whereas, in date(), a 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, whereas date() returns the 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 the 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 for day. 12 PHP idate() returns two digit integer for month. 11 PHP idate() returns four digit integer for year. 2021
Good luck and I hope this article can be useful. See you in the next article…