Wordpress注释时间戳中的用户本地时间

时间:2015-03-15 21:23:42

标签: javascript wordpress date timezone

我试图根据访问者的位置获取Wordpress评论以显示时间。因此,当访问者发表评论时,这是根据他们的时间。

由于不能单独使用PHP,我已经使用了Javascript。

<script type="text/javascript">
    var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()
    document.write("<b>" + day + "/" + month + "/" + year + "</b>")
</script>

当然,这只发布当前时间。如果我在get_the_time(下面的代码)中的注释中实现它,所有时间戳将同时读取访问者的当前时间。

PHP:

<div class="comment-time-stamp"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_the_time()) ?></div></div>

所以我尝试添加一个只影响访问者帖子的功能:

function local_date_time($format, $timestamp) {
    $timezone_str = get_option('timezone_string') ?: 'UTC';
    $timezone = new \DateTimeZone($timezone_str);

    // The date in the local timezone.
    $date = new \DateTime(null, $timezone);
    $date->setTimestamp($timestamp);
    $date_str = $date->format('Y-m-d H:i:s');

    // Pretend the local date is UTC to get the timestamp
    // to pass to local_date_time().
    $utc_timezone = new \DateTimeZone('UTC');
    $utc_date = new \DateTime($date_str, $utc_timezone);
    $timestamp = $utc_date->getTimestamp();

    return $format, $timestamp, true;
}

它打破了页面并阻止它加载。我有点迷失在哪里。

0 个答案:

没有答案
相关问题