Last updated on December 2, 2022
Today’s post is about Date time display change based on a given or supplied timezone, fortunately, it is very simple to convert one timezone to another timezone in PHP.
I am going to use PHP’s DateTime Class in this Example, you can use can achieve the same output with other methods.
Here is the example Script
$date = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $date->format('d-m-Y H:i:s');
$date = new DateTime('now', new DateTimeZone('America/New_York'));
echo $date->format('d-m-Y H:i:s');
$date = new DateTime('now', new DateTimeZone('Australia/Yancowinna'));
echo $date->format('d-m-Y H:i:s');
Here is the Simple Function to get a Date and time based on Given input Parameters, you can set the default Timezone and Date and time format for your requirement.
function getDateTime($timeZone = 'Asia/Kolkata',$format = 'd-m-Y H:i:s') {
$date = new DateTime('now', new DateTimeZone($timeZone));
return $date->format($format);
}
echo getDateTime(); // output : 15-04-2014 00:54:06
echo getDateTime('Australia/Yancowinna'); // output : 15-04-2014 04:54:06