9

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

Naftali
  • 144,921
  • 39
  • 244
  • 303
Jeremy John
  • 1,665
  • 3
  • 18
  • 31
  • If you know user timezone, then you can use `date_default_timezone_set('America/Los_Angeles');` – GBD Nov 12 '12 at 06:12
  • but that is only setting it to one zone i want it to be able to detect the user's zone of all type like +6gmt -8gmt etc all of it... – Jeremy John Nov 12 '12 at 06:15
  • have you stored each user timezone in DB ? – GBD Nov 12 '12 at 06:21
  • No the database timestap is all in UTC i want it to be like this: $item_date timestamp > PHP Date Format follow the user's computer time zone > output final PHP date... – Jeremy John Nov 12 '12 at 06:25

1 Answers1

26

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 ;).

hriziya
  • 1,116
  • 1
  • 23
  • 41
David Müller
  • 5,291
  • 2
  • 29
  • 33
  • Sorry i'm confused, where do i put the $item_date timestamp at? – Jeremy John Nov 12 '12 at 12:03
  • 1
    instead of time() in the setTimestamp() function – Elzo Valugi Nov 12 '12 at 12:07
  • the code works fine now but do you know how i can implement this into the code? http://stackoverflow.com/a/9531819/1700554 – Jeremy John Nov 12 '12 at 13:44
  • 1
    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. – David Müller Nov 12 '12 at 20:14
  • 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: http://codepad.org/efoZdNF4 http://ideone.com/Hw1CMk – Jeremy John Nov 13 '12 at 04:31
  • 1
    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. – David Müller Nov 13 '12 at 06:58
  • 2
    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);` – Jeremy John Nov 14 '12 at 00:38
  • The js file link is broken.. here is latest link for jstz.min.js http://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js I just updated answer with latest js function name in code and js file link – hriziya Dec 25 '13 at 09:39
  • That's pretty cool thanks @David, I was stuck on something similar, how'd you get around daylight saving time in the UK – Paul Ledger Oct 01 '14 at 18:09