PHP时间戳记日期到用户时区 [英] PHP timestamp date to user timezone

查看:113
本文介绍了PHP时间戳记日期到用户时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 如果(),我将从数据库中将原始生成的$ item_date的生成的mysql时间戳信息从数据库中拉出($ timestamp = strtotime($ item_date))=== false){
echo时间戳字符串是伪造的;
} else {
echo date('j M Y h:i:sA',$ timestamp);
}

输出服务器区域(UTC):


2012年11月12日05:54:11 PM


但我想要根据用户时区转换



示例:假设用户的时间是 2012年11月13日07:00:00(+0800 GMT),服务器时间为 2012年11月12日11:00:00 PM(UTC),$ item_date的时间戳记为 2012年11月12日10:30:00 PM(UTC)所以



具有(UTC)的用户将看到$ item_date:



< blockquote>

2012年11月12日10:30:00 PM


和用户(+0800 GMT)将请参阅$ item_date as:


2012年11月13日06:30:00 PM


我该如何完成?
谢谢

解决方案

这篇文章已经更新,包括一个完整的例子 / p>

 <?php 
session_start();

如果(isset($ _ POST ['timezone']))
{
$ _SESSION ['tz'] = $ _POST ['timezone'];
退出;
}

if(isset($ _ SESSION ['tz']))
{
//在这一点上,你的会话中有用户时区
$ item_date = 1371278212;

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

//只是为了乐趣:在UTC中会是什么?
$ dt-> setTimezone(new DateTimeZone(UTC));
$ will_be = $ dt-> format('Y-m-d H:i:sP');

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

echoTimestamp。 $ item_date。 是日期。 $是。
在用户时区。 $ dt-> getTimezone() - > getName()。
并将。 $ will_be。 在UTC< br />;
}
?>

< script type =text / javascriptsrc =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 (数据){
location.reload();
}
});

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

我希望这个现在已经足够清楚了);


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

解决方案

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

这篇关于PHP时间戳记日期到用户时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆