Reputation: 5706
i need to convert the given Australian times to UTC. this time can be from different states in AUS. i have the states in the DB and stored the time offset in a column.
i want to know how is possible to add a time offset or subtract it this way.
// stored values of states and its column.
"NT" = +9.30
"QLD" = +10.00
"WA" = +8.00
i am taking selected date time this way and need to reduce the offset.
$timeAinUTC = date("H:i A", strtotime("07am")) - (offset);
i.e $timeAinUTC = date("H:i A", strtotime("07am")) - (+9.30);
how can i get this kind of a work done with php's datetime ?
EDIT 1,
I tried this to add my offeset but it seems like i cannot provide double values like 2.30 hours or 2.50
It works when i ask it to add 2 hours this way.
strtotime($current_time . "+2hours");
but when i add like below , its wrong.
strtotime($current_time . "+2.30hours");
EDIT 2
All the sates available in AUS. ACT, NSW, NT, QLD, SA, TAS, VIC, WA
http://wwp.greenwichmeantime.com/time-zone/australia/time-zones/
EDIT 3
I tried this as below as explained by a person in the answer.
$date = new DateTime('2013-10-01 01:45', new DateTimeZone('Australia/Melbourne'));
echo $date->format('Y-m-d h:i:s A') . "<br>";
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Y-m-d h:i:s A') . "<br>";
But it outputs,
2013-10-01 01:45:00 AM
2013-09-30 03:45:00 PM
How can this be wrong ? 2013-09-30 03:45:00 PM is wrong as per my understaing and actually it should be 10 hours behind from 2013-10-01 01:45:00 AM to be UTC, isnt it ?
Upvotes: 5
Views: 21926
Reputation: 6135
I hope, below solution of changing timezone at run time will help you.
$date = new \DateTime(date('Y-m-d H:i:s'));
$date->setTimezone(new \DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d H:i:sP') . "<br>";
$date = new \DateTime(date('Y-m-d H:i:s'));
$date->setTimezone(new \DateTimeZone('UTC'));
echo $date->format('Y-m-d H:i:sP') . "<br>";
$date = new \DateTime(date('Y-m-d H:i:s'));
$date->setTimezone(new \DateTimeZone('America/Argentina/Salta'));
echo $date->format('Y-m-d H:i:sP') . "<br>";
Output when i checked:
2020-03-18 20:54:08+05:30
2020-03-18 15:24:08+00:00
2020-03-18 12:24:08-03:00
You can also set your own date instead of "date('Y-m-d H:i:s')".
Thanks for asking this question.
Upvotes: 0
Reputation: 9633
You can convert times like this:
<?php
$date = new DateTime('2013-10-01', new DateTimeZone('UTC'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Australia/Melbourne'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
PHP Supported timezones are listed here: http://www.php.net/manual/en/timezones.australia.php
Upvotes: 14