How To get The Current Date and Time in PHP

How do I get the current date and time in PHP? By using PHP's date function, which will return the server's current date and time.

echo date('d/m/Y H:i:s');

The date function will output:

03/05/2024 11:59:57

The parameters used in the above date example are as follows. Any other characters that don't match PHP's set date parameters are printed as is. In the above example that's "/" and ":".

'Y' - Four digit year
'm' - Two-digit month
'd' - Two-digit day
'H' - Hour 24-hour clock
'i' - Minute
's' - Second

If you already have a date that you need to convert you can use PHP's strtotime.

echo date('H:i:s') . "\n";
# Outputs
12:05:35

echo date('H:i:s', strtotime('+5 hours'));
# Outputs
17:05:35

Get the current date and time in PHP using the time function

We can use the time() function in PHP to get the Unix timestamp of the current date and time.

echo time();

Will output the current Unix timestamp of the server.

1714768590

We've built a free tool that takes a Unix timestamp and converts it to a human-readable date. If you are having trouble with dates in PHP you can also check the last triggered error with PHP's built-in get last date error, which will display an array of warnings and errors whilst parsing a date time string.

var_dump(date_get_last_errors());

Senior PHP developer with near two decades of PHP experience. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Related Dev Guides

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all