如何在一个页面上显示3个不同的datetime
个对象?以下代码仅返回服务器日期(第一项)并解除所有其他日期。
此脚本的目的是显示用户位置的当前日期和时间,包括夏令时(意味着特定地点的实际时间,而不仅仅是GMT +他们应该在的时区)。
$server_datetime = new DateTime(); //located in America/New_York timezone - GMT-5
echo "Current Server DateTime: " . $server_datetime->format('Y-m-d H:i:s');
echo "<hr>";
$usersTimezone1 = 'Europe/London'; // GMT-0
$user_datetime1 = new DateTime(new DateTimeZone($usersTimezone1) );
echo "London User DateTime: " . $user_datetime1->format('Y-m-d H:i:s');
echo "<hr>";
$usersTimezone2 = 'Asia/Shanghai'; // GMT+8
$user_datetime2 = new DateTime(new DateTimeZone($usersTimezone2) );
echo "Shanghai User DateTime: " . $user_datetime2->format('Y-m-d H:i:s');
echo "<hr>";
答案 0 :(得分:1)
消息:DateTime :: __ construct()期望参数1为字符串, 对象
DateTimeZone对象应该是第二个参数。
$server_datetime = new DateTime(); //located in America/New_York timezone - GMT-5
echo "Current Server DateTime: " . $server_datetime->format('Y-m-d H:i:s');
$usersTimezone1 = 'Europe/London'; // GMT-0
$user_datetime1 = new DateTime( 'now', new DateTimeZone($usersTimezone1) );
echo "London User DateTime: " . $user_datetime1->format('Y-m-d H:i:s');
$usersTimezone2 = 'Asia/Shanghai'; // GMT+8
$user_datetime2 = new DateTime( 'now', new DateTimeZone($usersTimezone2) );
echo "Shanghai User DateTime: " . $user_datetime2->format('Y-m-d H:i:s');
答案 1 :(得分:1)
$server_datetime = new DateTime(); //located in America/New_York timezone - GMT-5
echo "Current Server DateTime: " . $server_datetime->format('Y-m-d H:i:s');
echo "<hr>";
date_default_timezone_set('Europe/London');
$user_datetime = new DateTime();
echo "London User DateTime: " . $user_datetime->format('Y-m-d H:i:s');
echo "<hr>";
date_default_timezone_set('Asia/Shanghai'); // GMT+8
$user_datetime2 = new DateTime();
echo "Shanghai User DateTime: " . $user_datetime2->format('Y-m-d H:i:s');
echo "<hr>";
输出是:
Current Server DateTime: 2017-03-14 06:22:59
London User DateTime: 2017-03-14 05:22:59
Shanghai User DateTime: 2017-03-14 13:22:59
答案 2 :(得分:0)
@ Armitage2k试试这个:
<?php
$server_datetime = new DateTime();
echo "Current Server DateTime: " . $server_datetime->format('Y-m-d H:i:s');
echo "<hr>";
$usersTimezone1 = 'Europe/London'; // GMT-0
$user_datetime1 = new DateTime();
$user_datetime1->setTimeZone(new DateTimeZone($usersTimezone1));
echo "London User DateTime: " . $user_datetime1->format('Y-m-d H:i:s');
echo "<hr>";
$usersTimezone2 = 'Asia/Shanghai'; // GMT+8
$user_datetime2 = new DateTime();
$user_datetime2->setTimeZone(new DateTimeZone($usersTimezone2));
echo "Shanghai User DateTime:" . $user_datetime2->format('Y-m-d H:i:s');
echo "<hr>";