Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add timezone for a given time in php

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 ?

like image 976
dev1234 Avatar asked Dec 12 '22 11:12

dev1234


1 Answers

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

like image 186
LondonAppDev Avatar answered Dec 29 '22 15:12

LondonAppDev