|
-
Dec 13th, 2002, 01:01 PM
#1
Thread Starter
Addicted Member
DateDiff equiv in PHP
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.
-
Dec 15th, 2002, 03:47 PM
#2
Frenzied Member
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
-
Dec 15th, 2002, 11:39 PM
#3
Thread Starter
Addicted Member
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.
-
Dec 16th, 2002, 10:46 AM
#4
Frenzied Member
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.
-
Dec 16th, 2002, 10:55 AM
#5
Thread Starter
Addicted Member
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.
-
Dec 16th, 2002, 12:04 PM
#6
Frenzied Member
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?
-
Dec 16th, 2002, 12:24 PM
#7
Thread Starter
Addicted Member
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
-
Dec 16th, 2002, 12:30 PM
#8
Frenzied Member
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.
-
Dec 16th, 2002, 12:33 PM
#9
Thread Starter
Addicted Member
-
Dec 16th, 2002, 12:36 PM
#10
Frenzied Member
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?
-
Dec 16th, 2002, 12:47 PM
#11
Thread Starter
Addicted Member
Central Time Zone.
Here is some sample information that I get from the user:
12/16/02 9:47:25 AM
-
Dec 16th, 2002, 12:56 PM
#12
Frenzied Member
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.
-
Dec 16th, 2002, 01:06 PM
#13
Thread Starter
Addicted Member
What? lol. Sorry went over my head. Can you maybe write some sample code to help illustrate please?
-
Dec 16th, 2002, 01:44 PM
#14
Frenzied Member
you're kidding right 
if you didn't understand that than how are you going to write the code 
ok give me a bit and I will see what I can come up with.
-
Dec 16th, 2002, 01:47 PM
#15
Thread Starter
Addicted Member
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.
-
Dec 16th, 2002, 05:28 PM
#16
Frenzied Member
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 *** 
PHP Code:
<?
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.
-
Dec 16th, 2002, 05:35 PM
#17
Thread Starter
Addicted Member
Thanks
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?
PHP Code:
$Userzone = $serverTimezone $status $newzone2;
-
Dec 16th, 2002, 05:37 PM
#18
Frenzied Member
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.
-
Dec 16th, 2002, 05:38 PM
#19
Thread Starter
Addicted Member
I didn't know you could ommit the .
thanks.
-
Dec 17th, 2002, 02:11 PM
#20
Frenzied Member
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
PHP Code:
<?
$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
?>
-
Dec 17th, 2002, 03:37 PM
#21
Thread Starter
Addicted Member
Thank you! Thanks a lot phpman.
The site I am using this for is http://www.leetomatic.net
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|