Home > Blockchain >  PHP Converting DATE TIME TIMEZONE to DATE TIME NEW-TIMEZONE PHP
PHP Converting DATE TIME TIMEZONE to DATE TIME NEW-TIMEZONE PHP

Time:04-25

I have this php output from client that returns

Sat Apr 30 2022 19:23:03 GMT 0800 (China Standard Time)

I want that to convert in new timezone, the problem is I cannot control the data above sa I have to convert all of that output into new timezone which is Brisbane Timezone

Like this

$current_date_time_timezone = "Sat Apr 30 2022 19:23:03 GMT 0800 (China Standard Time)"
$timeBrisbane = new DateTimeZone('Australia/Brisbane');
$date_time_new_timezone = $date_time_new_timezone->setTimezone($timeBrisbane);
echo $date_time_new_timezone->format('Y-m-d H:i:s');

The output above returns error,

the output I want is like this:

output= "Sat Apr 30 2022 19:23:03 GMT 10 (Australia/Brisbane)"

CodePudding user response:

You can use DateTime::createFromFormat() to parse that initial string into a DateTime object.

Since the timezone offset is already there after "GMT" I’ll ignore the brackets part:

$current_date_time_timezone = "Sat Apr 30 2022 19:23:03 GMT 0800 (China Standard Time)";
$timeBrisbane = new DateTimeZone('Australia/Brisbane');

$date_time_new_timezone = DateTime::createFromFormat("D M j Y H:i:s \G\M\TO ", $current_date_time_timezone);
$date_time_new_timezone->setTimezone($timeBrisbane);

echo $date_time_new_timezone->format("D M j Y H:i:s \G\M\TO \(e\)");
// Sat Apr 30 2022 21:23:03 GMT 1000 (Australia/Brisbane)
  • Related