PHP timestamp date to user timezone

18,291

This post has been updated to include a full-fledged example

<?php
    session_start();

    if (isset($_POST['timezone']))
    {
        $_SESSION['tz'] = $_POST['timezone'];
        exit;
    }

    if (isset($_SESSION['tz']))
    {
        //at this point, you have the users timezone in your session
        $item_date = 1371278212;

        $dt = new DateTime();
        $dt->setTimestamp($item_date);

        //just for the fun: what would it be in UTC?
        $dt->setTimezone(new DateTimeZone("UTC"));
        $would_be = $dt->format('Y-m-d H:i:sP');

        $dt->setTimezone(new DateTimeZone($_SESSION['tz']));
        $is = $dt->format('Y-m-d H:i:sP');

        echo "Timestamp " . $item_date . " is date " . $is . 
             " in users timezone " . $dt->getTimezone()->getName() .
             " and would be " . $would_be . " in UTC<br />";
    }
?>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js"></script>
<script language="javascript">
  $(document).ready(function() {
        <?php if (!isset($_SESSION['tz'])) { ?>
            $.ajax({
                type: "POST",
                url: "tz.php",
                data: 'timezone=' + jstz.determine().name(),
                success: function(data){
                    location.reload();
                }
            });

        <?php } ?>        
    });
</script>

I hope this is now clear enough ;).

Share:
18,291
Jeremy John
Author by

Jeremy John

Hello World

Updated on July 28, 2022

Comments

  • Jeremy John
    Jeremy John almost 2 years

    I'm pulling the raw generated mysql timestamp info of $item_date from the database as php date format:

    if (($timestamp = strtotime($item_date)) === false) {
        echo "The timestamp string is bogus";
    } else {
        echo date('j M Y h:i:sA', $timestamp);
    }
    

    Output folowwing the server zone (UTC):

    12 Nov 2012 05:54:11PM

    but i want it to convert according to the user time zone

    Example: let's say if the user's time is 13 Nov 2012 07:00:00 AM(+0800 GMT) and the server time is 12 Nov 2012 11:00:00 PM(UTC) and the timestamp of $item_date is 12 Nov 2012 10:30:00 PM (UTC) so

    User with (UTC) will see $item_date as:

    12 Nov 2012 10:30:00 PM

    and user with (+0800 GMT) will see $item_date as:

    13 Nov 2012 06:30:00 PM

    How do i get it done? Thanks

  • Jeremy John
    Jeremy John over 11 years
    Sorry i'm confused, where do i put the $item_date timestamp at?
  • Elzo Valugi
    Elzo Valugi over 11 years
    instead of time() in the setTimestamp() function
  • Jeremy John
    Jeremy John over 11 years
    the code works fine now but do you know how i can implement this into the code? stackoverflow.com/a/9531819/1700554
  • David Müller
    David Müller over 11 years
    Hey there, I have updated the post to include a complete example that should make you happy now ;). Please mark the post as accepted, if this answers your question entirely.
  • Jeremy John
    Jeremy John over 11 years
    Thanks for the reply, I understand $item_date = 1371278212 is the timestamp but my mysql generated time timestamp from phpmyadmin is in time format: 2012-11-12 23:15:20 and i tested the exact code but got a reloading blank page.. tried with a fiddle too: codepad.org/efoZdNF4 ideone.com/Hw1CMk
  • David Müller
    David Müller over 11 years
    Hey there, we are getting closer ;). Use DateTime::createFromFormat to initialize from your mysql date. Do it like this: $dt = DateTime::createFromFormat("Y-m-d H:i:s", $item_date); and remove the call $dt->setTimestamp($item_date);. Be sure to save the page as tz.php, as the ajax call is invoking that very page.
  • Jeremy John
    Jeremy John over 11 years
    Hey Thanks for helping me until this far, it's now working! My host is using PHP 5.2 so DateTime::createFromFormat does not work but i used this to work around it: $item_date = '2012-11-12 23:15:20'; with $dt = new DateTime($item_date);
  • hriziya
    hriziya over 10 years
    The js file link is broken.. here is latest link for jstz.min.js cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/… I just updated answer with latest js function name in code and js file link
  • Paul Ledger
    Paul Ledger over 9 years
    That's pretty cool thanks @David, I was stuck on something similar, how'd you get around daylight saving time in the UK