Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP timestamp date to user timezone

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

like image 879
Jeremy John Avatar asked Nov 12 '12 06:11

Jeremy John


People also ask

How to fetch time from DateTime in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How to echo time PHP?

PHP time() Functionecho(date("Y-m-d",$t));

How to get current date timestamp in PHP?

You can either use the $_SERVER['REQUEST_TIME'] variable (available since PHP 5.1. 0) or the time() function to get the current Unix timestamp. It's worth noting that the timestamp returned by the time() function is independent of the timezone.


1 Answers

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

like image 94
David Müller Avatar answered Oct 11 '22 06:10

David Müller