Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Weird strange error with getTime and PHP

    (I must note that this stuff used to work when I first coded and tested it a number of years ago and for some reason it isn't working anymore)

    So I'm trying to store the time difference between the end user's computer and the server time so that entries in a journal can be synchronized to their current time zone.

    To do this I have a javascript function that basically gets the Javascript timestamp since the epoch and submits it via a form which is then manipulated using PHP.

    However some very STRANGE things are going on:

    1) The timestamp when compared to the PHP timestamp at the exact same moment is only about 500 seconds off. So for example the PHP timestamp will say something like 124532134000 and the javascript one will be 124532134500. This difference appears to be constant. My computer time is about 14 hours different than the time on the server, so this makes no sense.

    2) When I take the javascript timestamp and assign it using setTime and then spit out getDate, hour, seconds etc it spits out, perfectly, my current time and date on my client computer. When I just spit out getDate, hour, seconds etc I also get the proper time on my computer.

    3) When I take that exact timestamp from javascript and throw it into the PHP date() function I get a time that is again about 500 seconds off server time which is about 14 hours off from where Javascript seems to think it is.

    Therefore, for some very strange reason Javascript seems to think the epoch occurred at a different time than PHP, and I cannot figure out why this is happening.

    I have tested in ie, chrome and firefox, same deal. I tried using that GMToffset function to see if it was somehow messing up the timestamp but no, that wouldn't make any sense given what I've said above.

    So basically, my question is:

    When I seed PHP with the same timestamp I feed Javascript, why do I get a different output date? (ie, why does javascript's epoch offset seem to differ from PHP's?)

  • #2
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, and yeah, I didn't mention but I am aware of the milisecond and second difference and this has already been taken into consideration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,384 Times in 4,349 Posts
    Why in the world would you assume that the clock on the server and the clock on your computer are in any way synchronized???

    If you have two clocks in your house, do they agree on the time, to the second? Of course not, unless they are somehow synchronized to some master standard clock via radio.

    Computer clocks, in general, are *NOT* synchronized in any way. Oh, there may be some crude attempt to keep them in sync from time to time, but 500 milliseconds difference? That's noise. Heck, it's quite possible for there to be 500 milliseconds LATENCY in communications from one computer to another.

    EDIT: I just checked. There are a shade over 3 seconds difference between the time on my computer and the time on one of the servers I use. And a shade under 4 seconds difference compared to another server.
    Last edited by Old Pedant; 07-10-2013 at 07:44 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow.

    Did you read the post?

    Perhaps I should let you find your own misunderstanding prior to pointing it out, since you are a "SUPREME CODER".

    I mean, I know the issue I am addressing appears to be a little complicated, but being a coder the assumption that we can comprehend posts, take the time to read them and actually engage ourselves in an interesting dilemma.

    So yeah, don't assume everyone is retarded. And maybe take the time to read posts in their entirety.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,384 Times in 4,349 Posts
    OH WOW! I did misread it! I read it as 500 *MILLI*seconds difference, not 500 seconds. And I read that at least 3 times, and still misread it.

    SHEESH! Old age and blindness strikes again.

    500 seconds just doesn't make any sense, I totally agree.

    I could see a difference of one hour, perhaps the difference between DST and non-DST, for example. But 500 seconds is just too weird to believe.

    Now you're going to make me actually try it!

    I don't normally use PHP, but I do have it installed on one server, so I'll try it there.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,384 Times in 4,349 Posts
    Okay, I tried it.

    Nope, not happening for me.

    I see a 3 second difference, but that's all.

    I just did this for the php:
    Code:
    <?php
    echo date( DATE_RSS ) . "<hr/>";
    echo date( DATE_RSS, (int) $_GET["tm"] ) . "<hr/>";
    ?>
    And then hit the page with ttest.php?tm=xxxx where I generated the xxxx from JavaScript via:
    Code:
    <script>
    var now = new Date();
    var sec = Math.round(now.getTime()/1000);
    location.href = "ttest.php?tm=" + sec;
    </script>
    Consistently a 3 second difference.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,384 Times in 4,349 Posts
    Forgot to mention: The server is 2 hours different from me (different time zone), so in actuality the two times displayed are 2 hours and 3 seconds different. But, as you said, that part makes sense. And the 3 seconds makes sense. But 500 seconds???

    And I'm assuming that the timestamp numbers you showed were totally fictitious, because the numbers I am seeing, for today, are something like 1373501415.

    That is: 1,373,501,415
    Versus your: 124,532,134,000
    which is around 100 times too large. (Has to be: won't fit into 32 bits.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the confirmation - yeah, this is how it used to work. But it simply does not translate. The PHP date() function, when seeded with the exact same timestamp that javascript spits out, gives me a date about 14 hours off.

    I found a way to circumvent it which is to submit the actual date in written format (day, hour, minute, month, year) via javascript to PHP then use the strtotime() function to create a PHP timestamp which converts perfectly.

    I've tested this in numerous different ways and for some reason the Javascript timestamp does not work properly.

    I used your script to test, btw, and although I'm getting a better solution than I was before it is still slightly off.

    Wed, 10 Jul 2013 20:05:39 -0400
    Wed, 10 Jul 2013 20:14:38 -0400

    No clue why it's different now - I was doing it via a form and it was bugging up. Anyways, I'm not going to mess with it because I have my hacked solution. :-)

    Thanks, and sorry for being snippy.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,384 Times in 4,349 Posts
    Definitely something weird going on in your PHP server, I would say.

    By the by, you said:
    Code:
    although I'm getting a better solution than I was before it is still slightly off.
    Wed, 10 Jul 2013 20:05:39 -0400
    Wed, 10 Jul 2013 20:14:38 -0400
    Ummm...8 minutes and 59 seconds off would be 539 seconds off. Seems to me to be essentially the same as what you said was happening before.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, what was happening before is when I fed the javascript to the PHP it was giving me 500 seconds off from the server time, which means like 14 hours off from the client time.

    So say I output time() via php and document.write.date.getTime() - those would only be about 500 seconds off.

    Now it appears it is about 500 seconds off from the client time.

    I don't think it is the PHP - it has to be the javascript, but I have no idea why. Unless the PHP version I am running has some problem with the epoch.


  •  

    Posting Permissions

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