Time with Timezone

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sarav_dude
Forum Newbie
Posts: 24
Joined: Sun Apr 30, 2006 8:40 am
Location: India
Contact:

Time with Timezone

Post by sarav_dude »

Hi there,

I have a selection box in which a list of times are available like

1:00,
2:00,
......,
23:00,
00:00

and another select box with the timezones.

user selecting this time, timezone and it is stored in the DB.

Now, suppose if a user has selected 22:00 and time zone selected is (US/Alaska) how do i show this time in (Asia/Calcutta) ?

Also, as We know we can use the following

putenv('TZ=Asia/Calcutta');
echo date("h:i",time());

but how do i convert 22:00 to timestamp.

Thanks.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

User avatar
sarav_dude
Forum Newbie
Posts: 24
Joined: Sun Apr 30, 2006 8:40 am
Location: India
Contact:

Post by sarav_dude »

i have used mktime() already , it shows the same time in different environments
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

sarav_dude wrote:i have used mktime() already , it shows the same time in different environments
Use it to calculate the difference between the times, and then apply that difference to all time values.
User avatar
sarav_dude
Forum Newbie
Posts: 24
Joined: Sun Apr 30, 2006 8:40 am
Location: India
Contact:

Post by sarav_dude »

Please read my question carefully ... i want this following working but it is not working :

Code: Select all

putenv('TZ=Africa/Harare');

$ts = mktime(22, 0, 0, date('n'), date('j'), date('Y')); 
echo date("h:i",$ts)."<br />";


putenv('TZ=Asia/Calcutta');
$ts = mktime(22, 0, 0, date('n'), date('j'), date('Y')); 
echo date("h:i",$ts)."<br />";

It should display the time according to that in different TZ variables.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

I'm not sure if mktime() works well with the putenv the TZ, but if i was at your place i would use strtotime(), it works very well with TZ and you don't need to specify bunch of arguments, but just

Code: Select all

strtotime(date('Y-m-d H:i:s'));
User avatar
sarav_dude
Forum Newbie
Posts: 24
Joined: Sun Apr 30, 2006 8:40 am
Location: India
Contact:

Post by sarav_dude »

ok good , but how we can use this with this time 22:00 ,say i want to show 22:00 in two time zones then ?
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Use the above example and do it yourself, we are not here to do any coding work, just give little help.

In order to display 22:00 in 2 time zones you need first to set the one timezone with putenv then execute strtotime which returns the result in variable, lets say $time_1, then run again putenv with the other time zone and again strtotime which returns the result in $time_2.

echo "Time zone 1:".$time_1.' Time zone 2: '.$time_2;
User avatar
sarav_dude
Forum Newbie
Posts: 24
Joined: Sun Apr 30, 2006 8:40 am
Location: India
Contact:

Post by sarav_dude »

Yes its great ... thats what i need thanks buddy u are awesome :D :D :D

Code: Select all

putenv('TZ=Asia/Calcutta');
$time=strtotime('22:00');
echo date("h:i:s a",$time);
putenv('TZ=Africa/Harare'); 
echo date("h:i:s a",$time);
Post Reply