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();
$datetime->setTimezone($timezone);
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. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close