Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert into local time 1

Status
Not open for further replies.

spookie

Programmer
May 30, 2001
655
IN
Hi,

I have a task to convert CET time (e.g.18:00 CET) to local time depending on the timezone provided(e.g.Europe/Vienna, US/Eastern etc). The conversion should also take into consideration the day light saving.
The input will be hh:mm e.g.18:00 and the ouput should be in the similar format. I am out of touch for this and need help here.

Any guidance is highly appreciated.





--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
the code below requires $time to be in the format '18:00 CET' or '6:00 PM CET'. i.e. it requires that CET be present. if this is not the case, test for it and add it to the string before using it in your code.

Code:
date_default_timezone_set('EUROPE/Vienna') //eg
$localtime = date('H:i:s T', strtotime($time));
echo $localtime;
 
Thanks japadie.

Below is my test code
Code:
<?php
date_default_timezone_set('EUROPE/Vienna');
$time = "18:00 CET";
$localtime = date('H:i:s T', strtotime($time));
echo $localtime;
?>
and the output is 19:00:00 CEST. So it is working.

The task has changed though somewhat :)

I have a script which does some planned work according to the local time i.e.CET. Now I will be receiving the data in the format of device no and the different timezones where the device actually belongs to.
The planned work is going to be 18:00 to 8:00 as per the device country. I have to convert the hardcore 18:00 and 8:00 to CET and dump it in DB, so that the script runs as normal. So in this case my code will look something like
Code:
<?php
date_default_timezone_set('EUROPE/Berlin');
$time = "18:00 CEST";
$localtime = date('H:i:s T', strtotime($time));
echo $localtime;
?>
where the CEST part of
Code:
$time = "18:00 CEST";
will be dynamic as per the actual timezone. Is there any way I can determine that part as per the timezone given? Also is the above approach takes care of day light savings in all the countries by default.

Thanks in advance for your help.



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
I see. how about this then

Code:
echo getCET('18:00', 'UTC'); //can be any valid timezone identifier EUROPE/Paris GMT etc etc
function getCET($time, $timeZone){
	date_default_timezone_set('EUROPE/Berlin');
	return date('H:i:s O', strtotime("$time $timeZone"));
}

yes, these functions take account of daylight savings time providing that the rules are properly propagated through the zoneinfo into the local TZ databases. there is a potential anomaly in that the zoneinfo rules are not clever enough to record a change in rules as applicable from a date in time. so if the rules of calculating DST for a territory change, then there is the potential for misparsing older time stamps. This should not be relevant to your issues however, which are all in the here and now.
 
Thanks jpadie.

yes, these functions take account of daylight savings time providing that the rules are properly propagated through the zoneinfo into the local TZ databases.

Is it server admin's responsibility to see if the rules are propagated properly. Can this be checked somehow that this is fine.




--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Hi jpadie,

Code:
<?php
echo getCET('18:00', 'EUROPE/Berlin'); 
function getCET($time, $timeZone){
    date_default_timezone_set('EUROPE/Berlin');
    return date('H:i:s O', strtotime("$time $timeZone"));
}
?>
for the above code, the output should be 18:00:00 as both the timezones are equal (EUROPE/Berlin).
But i am getting output as '01:00:00 +0100'. I could not understand why its giving me this output. Can you please explain.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
bizarre.

the timezones are case sensitive (apparently). convert them to lower case and all works fine. if you cannot guarantee the case of $timeZone, then use strtolower() to do the necessary
 
Code:
<?php
echo getCET('18:00', 'EUROPE/Berlin'); 
function getCET($time, $timeZone){
    date_default_timezone_set('Europe/Berlin');
    return date('H:i:s O', strtotime("$time " .strtolower($timeZone));
}
?>
 
Hi jpadie,

Still no luck..
Code:
<?php
echo getCET('18:00', 'europe/berlin'); //can be any valid timezone identifier EUROPE/Paris GMT etc etc
function getCET($time, $timeZone){
    date_default_timezone_set('europe/berlin');
    return date('H:i:s O', strtotime($time .'europe/berlin'));
}
?>
still gives the same output i.e.01:00:00 +0100
Code:
<?php
echo getCET('18:00', 'GMT'); 
function getCET($time, $timeZone){
    date_default_timezone_set('europe/berlin');
    return date('H:i:s O', strtotime("$time " .strtolower($timeZone));
}
?>
gives output 20:00:00 +0200.

I doubt is it server configuration problem for timezones..

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
this is curious. it was working for me the other day, but not today. there must be something i did differently...

this code is working for me today

Code:
echo getCET('18:00', 'GMT'); 
function getCET($time, $timeZone){
    date_default_timezone_set('Europe/Berlin');
    return date('H:i:s O', strtotime("$time " .ucwords($timeZone)));
}
 
Hi jpadie,

The above code is working for me as well. :)
Just trying to know more about the local time conversion. I will get back with some more queries :)

Thanks for your help.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top