Click to See Complete Forum and Search --> : DateDiff equiv in PHP
OsirisX
Dec 13th, 2002, 12:01 PM
I'd like to take a time given by the user (supposedly the current time) and find out the time zone of it. I am thinking I'll use the correct CST and somehow compare them. Any ideas?
Thanks.
phpman
Dec 15th, 2002, 02:47 PM
you would have to get the time zone of the user and subtract it from the servers timezone. all of this can be done with the date() function.
somehting like this
$timeoffset = intval(date("H",12*60*60)-gmdate("H",12*60*60));
date(h:i A, time()+(-8-$timeoffset)*3600);
-8 = the users timezone
OsirisX
Dec 15th, 2002, 10:39 PM
Thank you but I have the opposite problem. I will be given a time and date of the user and I want to figure out what their time zone is.
phpman
Dec 16th, 2002, 09:46 AM
well, you could try this
date("M-d g:ia T");
the T will print out the timezone but I believe it is for the servers timezone. also how do you plan on getting the users current time without knowing there timezone? if you use date to get the users time I don't think that will work, it is getting teh servers time.
OsirisX
Dec 16th, 2002, 09:55 AM
The date is provided by their system from AIM. That isn't important here though. I am getting the time from the user, and I'd like to determine what time zone that they are in based on the time provided. This should be possible. I just need to compare the time given with the correct time. Analyze the hour offset.
phpman
Dec 16th, 2002, 11:04 AM
well you could try localtime.
ifyou already have the time from the user then you need to subtract it like you said, but how would you know to subtract it or add it?
OsirisX
Dec 16th, 2002, 11:24 AM
I am not sure. Maybe I could do this with substr and strpos. Just getting the hour that the user has given me, and comparing it. Hmm
phpman
Dec 16th, 2002, 11:30 AM
strpos and substr aren't going to tell you anything. you will need to know if they are forward or backward from your server.
just getting the time I don't believe you could do this. are they a dat ahead or a day behind. you don't have enough info just from the time.
OsirisX
Dec 16th, 2002, 11:33 AM
I have their date too.
phpman
Dec 16th, 2002, 11:36 AM
ok that will solve one problem, but now you ned to figure out which way to go, forward or backward. where is the server located?
OsirisX
Dec 16th, 2002, 11:47 AM
Central Time Zone.
Here is some sample information that I get from the user:
12/16/02 9:47:25 AM
phpman
Dec 16th, 2002, 11:56 AM
ok now check this out.
you have this info
12/16/02 9:47:25 AM
and you know the server is -5 I believe correct?
now if it was me that sent that time it would be easy, I am at -8 and that is my time you show, so the servers time is 12:47 I think
so you know that I am 3 hours slower than the server so you can just add -3 to the servers timezone.
so you would need to get the date and then if it is a day ahead you know you have to go back so you would add a postivie number, if the date is the same then you have to add a negative number. also you would need to see which way the time is, above or below the servers time.
I think that should work if I have my math right.
OsirisX
Dec 16th, 2002, 12:06 PM
What? lol. Sorry went over my head. Can you maybe write some sample code to help illustrate please?
phpman
Dec 16th, 2002, 12:44 PM
you're kidding right :)
if you didn't understand that than how are you going to write the code :p
ok give me a bit and I will see what I can come up with.
OsirisX
Dec 16th, 2002, 12:47 PM
Well I understand some of what you said, I understand the concept, but I don't see how I'm supposed to gather and compare the data. Thank you. :)
phpman
Dec 16th, 2002, 04:28 PM
ok, this is very rough and not tested. I don't know how you got the date and time from the user but you can edit it from here. try it and let me kow if it works. I just pulled this out of my *** :p
<?
echo "localtime: ". localtime();
$usrtime = "12/16/02 9:47:25 AM";
$newusertime = strtotime($usrtime);
$serverTime = time();
$serverTimezone = intval(date("H",12*60*60)-gmdate("H",12*60*60));
$newzone = floor ($serverTime - $usrtime) / 3600); // 3600 = 1 hour
if ($usrtime > $serverTime){
$status = "-";
} else {
$status = "+";
}
$Userzone = $serverTimezone $status $newzone2;
?>
I will also play around with it when I have time.
OsirisX
Dec 16th, 2002, 04:35 PM
I appreciate it. I'll try it when I get a chance.
BTW: I'm kinda new to php but not new to programming. What does php do when you put variables just straight next to each other with a space like you did on the last line?
$Userzone = $serverTimezone $status $newzone2;
phpman
Dec 16th, 2002, 04:37 PM
it should just print out ok, if not add a . in between them. generally I don't have a problem but sometimes php gets picky. :)
OsirisX
Dec 16th, 2002, 04:38 PM
I didn't know you could ommit the .
thanks.
phpman
Dec 17th, 2002, 01:11 PM
this is more of what you want. it is close but still has problems, goes past +12 and -12 so you can edit it like you want to get the correct Time zone
<?
$usrtime = "12/17/02 11:05:25 AM";
$newusertime = strtotime($usrtime);
$serverTime = time();
$serverTimezone = intval(date("H",12*60*60)-gmdate("H",12*60*60));
$newzone = floor(($serverTime - $newusertime) / 3600);
if ($newusertime > $serverTime){
$status = "+";
} else {
$status = "-";
}
$zone = $status . $newzone;
$Userzone = floor($serverTimezone + $zone);
echo "Users Timezone is: ".$Userzone
// 3600 = 1 hour
?>
OsirisX
Dec 17th, 2002, 02:37 PM
Thank you! Thanks a lot phpman.
The site I am using this for is http://www.leetomatic.net
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.