Results 1 to 21 of 21

Thread: DateDiff equiv in PHP

  1. #1

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132

    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.
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  2. #2
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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

  3. #3

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132
    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.
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  4. #4
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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.

  5. #5

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132
    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.
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  6. #6
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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?

  7. #7

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132
    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
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  8. #8
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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.

  9. #9

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132
    I have their date too.
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  10. #10
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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?

  11. #11

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132
    Central Time Zone.
    Here is some sample information that I get from the user:
    12/16/02 9:47:25 AM
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  12. #12
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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.

  13. #13

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132
    What? lol. Sorry went over my head. Can you maybe write some sample code to help illustrate please?
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  14. #14
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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.

  15. #15

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132
    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.
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  16. #16
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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.

  17. #17

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132

    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
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  18. #18
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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.

  19. #19

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132
    I didn't know you could ommit the .
    thanks.
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

  20. #20
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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

    ?>

  21. #21

    Thread Starter
    Addicted Member OsirisX's Avatar
    Join Date
    May 2002
    Location
    Montgomery, AL
    Posts
    132
    Thank you! Thanks a lot phpman.
    The site I am using this for is http://www.leetomatic.net
    --OsirisX
    AIM, Yahoo: OsirisX11
    ICQ: 56697525
    MSN: [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width