SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    time correction help

    can anyone show me the best way of getting the users local time instead of the server time...
    my host is in the US and i'm in the UK whenever anything is done on my site it displays the time as the US's time so am assuming thats the server's time.

    How would i go about using local time instead?

    thanks in advance

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2007
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this :

    <?
    echo "Original Time: ". date("h:i:s")."\n";
    putenv("TZ=US/Eastern");
    echo "New Time: ". date("h:i:s")."\n";
    ?>

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the replies... i have an odd problem occuring... when i use php's time() function it returns a date and time in 1969 i think it was.... any thoughts? I haven't been able to test any of the sugesetions above becaue this has bugged me for a while...

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's likely you're using a date function incorrectly, where you're probably passing an invalid value where a function wants a valid unix timestamp as the argument. The php manual usually provides documentation, including examples, of how to use the various date functions.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm, i don't know...i don't think so. What i've done is :
    $time=time();
    $time is inserted in my database then one another page i've used
    :
    echo 'Last Saved at: ', date(DATE_ATOM, $timestamp);
    Time stamp is the value of $time that was inserted in the database
    If there is anything going wrong it must be with DATE_ATOM but i don't think so because i used date(DATE_ATOM, $timestamp) exactly as it is in my google sitemap and its generates a w3c time and date properly. Or is there something i'm confusing for another, i can't see it...

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i've tried using
    strftime("&#37;c",$timestamp)
    but it still returns the same thing... My guess now then is that there is a problem with the time stamp generated by time()...its all i can see going wrong, not sur ehow though.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the exact value generated by the time() function?

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're making multiple assumptions of what you think certain values and variables in your script/database are. You should check them. Check them at various points throughout your program to narrow down to the problem area. var_dump() is very effective for this. A good place to start checking is when you call the data function and feed the unix timestamp to it. You will find that it is not what you think it is.

  10. #10
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    true...i did make a lot of assumptions. guess it frustrated me and still does. One of the unix timestamps generated by time() is
    1237156737
    i checked this using an online timestamp to date converter and i got a date and time from it. i then checked the value of $timestamp being fed into the date function and it is retrieving the correct values from the database to put into the function...i don't know whats wrong.

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code yields what output?
    PHP Code:
    echo date(DATE_ATOM'1237156737'); 

  12. #12
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, even strftime("&#37;c",'1237156737') they both produce a 1970 date wasn't 1969 as i quoted earlier.

  13. #13
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about using an integer?
    PHP Code:
    echo date(DATE_ATOM1237156737); 
    What version of php? I know a long time ago mktime() would only accept integers, even if they were strings which could be properly converted to ints. Maybe date had a similar problem.

  14. #14
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it was php 5.1.2 i managed to sort it. It may have been a bug with that version of php or with my installation. It was really frustrating me because i couldn't find anything wrong. I installed php 5.2.6 and it now works properly. I used SJH's suggestion in my header file date_default_timezone_set('UTC') and it now gets the correct time zone. It still displays the UK time an hour behind what my computer says but i think that has something to do with where i am in london or something, and all the GMT time stuff. Had my friend in the US post a blog entry too and it posted the correct US time so it is all working now. As far as i can tell anyway, no problems. Thank you for replying and makign suggestions, especially you crmalibu all your help was really appreciated.

  15. #15
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you observing daylight savings time at the moment? That could account for the 1hr difference from UTC.
    You could use Europe/London instead of UTC, but I haven't read this thread closely, it may be that UTC is what you actually want


Bookmarks

Posting Permissions

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