Skip to main content

Get Current Date Time and Timestamp with Defined Format in PHP

In PHP, retrieving the current date and time is a matter of using the right built-in function. As of PHP 5.2, the DateTime class offers an object-oriented way of dealing with dates and times.

If you just need to output the current date and time in a predefined format, you can use the date() function.

The following example outputs the current date and time in the different format:

print date("d");
print date("m");
print date("Y");
print date("m-d-Y");
print date("Y-m-d H:i:s");
//PHP 5.2+
$timezone = new DateTimeZone('EDT');
$datetime = new DateTime();
print $datetime->format('Y\-m\-d\ h:i:s');
//current timestamp
print time();

By continuing to use the site, you agree to the use of cookies.