In general, we use the date()
function to get date and time in PHP. It returns the time based on the timezone set in the PHP server. You can get the DateTime from the timezone set as default on the PHP configuration. In this example code, we will show you how to get the current time by given a timezone in PHP. You can get and display the current Date and Time from a specific timezone using PHP.
Use the following code snippet to get the current date by America/New_York timezone and display DateTime in YYYY-MM-DD HH:MM:SS format using PHP.
$date = new DateTime("now", new DateTimeZone('America/New_York') );
echo $date->format('Y-m-d H:i:s');
You can change the timezone in the DateTimeZone
object and adjust the date format in the format()
function.