PHP Current Date

by Vincy. Last modified on July 8th, 2022.

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 the first two of the above list while seeing about PHP date functions. Let us see these functions with some more details with examples.

PHP date()

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.

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.
PHP date() with date constants returns.

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.

print "<br/><br/><b>PHP getdate() returns current date components.</b><br/>";
$current_date_components = getdate();
print "<PRE>";
print "</PRE>";

The array of date components that are returned by this program is,

PHP getdate() returns current date components.
    [seconds] => 19
    [minutes] => 9
    [hours] => 6
    [mday] => 16
    [wday] => 5
    [mon] => 8
    [year] => 2013
    [yday] => 227
    [weekday] => Friday
    [month] => August
    [0] => 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,

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.


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.

For example,

print "<br/><br/><b>PHP getdate() returns current date components.</b><br/>";
$current_date_components = idate("Y");
print "<PRE>";
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.
PHP idate("m") returns integer represents month.
PHP idate("Y") returns four digit integer represents year.

Download PHP Current Date Source Code

Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP Current Date”

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page