How to get Current DateTime by given a Timezone in PHP

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.

  • Create a new DateTime object with PHP.
  • Convert the DateTime object to the timezone of New_York (UTC-5) using the DateTimeZone object.
  • Specify the date and time format.
$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.

Leave a reply

keyboard_double_arrow_up