Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Wordpress注释时间戳中的用户本地时间_Javascript_Wordpress_Date_Timezone - Fatal编程技术网

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

Javascript Wordpress注释时间戳中的用户本地时间,javascript,wordpress,date,timezone,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 =

我正试图通过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>

它弄坏了这一页,使它无法加载。我有点不知所措。

在您的第一个脚本中,只需将
new Date()
替换为
new Date(HPProviderd Datestamp)
如果我使用第一个代码并添加
HPProviderd Datestamp
,它不是会给出发布的所有评论(新的和旧的)当前日期吗?与类似于
get\u comment\u date
的东西相反。好吧,这不完全是一个答案,但到目前为止似乎有效!有一个Wordpress插件可以根据位置自动调整站点日期和时间:()我只是说你可以通过new date()将评论日期输入javascript,代码将按照你所描述的那样工作,但是使用的是评论日期而不是当前日期。我一定是错误地实现了你的建议。如果我在
get\u comment\u date()
之外使用建议的javascript,那么所有帖子的时间都会重复。如果我在内部使用它,它会错误地生成所有可能的时间:
get\u comment\u date('var currentDate=new date(hpprovideddatestamp)var day=currentDate.getDate()var month=currentDate.getMonth()+1 var year=currentDate.getFullYear()文档。编写(“+day+”/“+month+”/“+year+”+year+)
<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;
}