我正在从数据库中提取 $item_date 的原始生成的 mysql 时间戳信息作为 php 日期格式:
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);
}
服务器区域 (UTC) 之后的输出:
Output folowwing the server zone (UTC):
2012 年 11 月 12 日下午 5:54:11
12 Nov 2012 05:54:11PM
但我希望它根据用户时区进行转换
but i want it to convert according to the user time zone
示例:假设用户的时间是 13 Nov 2012 07:00:00 AM(+0800 GMT) 并且服务器时间是 12 Nov 2012 11:00:00 PM(UTC) 并且 $item_date 的时间戳是 2012 年 11 月 12 日晚上 10:30:00 (UTC) 所以
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
(UTC) 的用户将看到 $item_date 为:
User with (UTC) will see $item_date as:
2012 年 11 月 12 日晚上 10:30:00
12 Nov 2012 10:30:00 PM
使用 (+0800 GMT) 的用户将看到 $item_date 为:
and user with (+0800 GMT) will see $item_date as:
2012 年 11 月 13 日下午 6:30:00
13 Nov 2012 06:30:00 PM
我该如何完成?谢谢
这篇文章已经更新,包含一个完整的例子
<?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时间戳日期到用户时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!