如何在当地时区显示strtotime

时间:2015-02-22 09:06:15

标签: php timezone

使用聊天框,其中获取带有原始的msg时间到目前为止我已经有$msg_time = strtotime($row["date_time"]);正确显示strtotime值。现在变得混乱如何用当地时区显示这个

1 个答案:

答案 0 :(得分:1)

服务器时区

     <?php
        // set the default timezone to use. Available since PHP 5.1
        date_default_timezone_set('UTC');

        echo date("m-d-Y", strtotime('2014-02-21')) . "timezone: " . date_default_timezone_get();

客户时区

要显示时区的文件

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Title</title>

    <!-- javascript/jQuery -->
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

    <?php
        session_start();
        echo isset($_SESSION['time']) ? $_SESSION['time'] : "error";
    ?>
</head>
<body>

<script type="text/javascript">
    $(document).ready(function () {
        var visitortime = new Date();
        var visitortimezone = "GMT " + -visitortime.getTimezoneOffset() / 60;
        $.ajax({
            type: "POST",
            url: "timezone.php?XDEBUG_SESSION_START=1",
            data: 'time=' + visitortimezone,
            success: function (data) {

            }
        });
    });
</script>
</body>
</html>

timezone.php

<?php
session_start();
$_SESSION['time'] = $_POST['time'];