How to get the current Date and Time in PHP ?


In PHP, you can get the current date and time using the date() function. The date() function formats a timestamp to a more readable date and time.

Here are some examples of how to get the current date and time in PHP:

  • Get the current date and time in the default format:
$currentDateTime = date('Y-m-d H:i:s');
echo $currentDateTime;

Output: 2021-10-14 14:30:00

  • Get the current date and time in a custom format:
$currentDateTime = date('l jS \of F Y h:i:s A');
echo $currentDateTime;

Output: Thursday 14th of October 2021 02:30:00 PM

  • Get the current date and time as a Unix timestamp:
$currentDateTime = time();
echo $currentDateTime;

Output: 1634220600

Note: The Unix timestamp represents the number of seconds since January 1, 1970, 00:00:00 UTC.

  • Get the current date and time in a specific timezone:
date_default_timezone_set('America/New_York');
$currentDateTime = date('Y-m-d H:i:s');
echo $currentDateTime;

Output: 2021-10-14 10:30:00

Note: The date_default_timezone_set() function sets the default timezone used by all date/time functions in the script. In this example, the timezone is set to "America/New_York".



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.