简体   繁体   English

DateTime输入在PHP中的时区之间转换

[英]DateTime input convert between timezones in PHP

I have an input box that grabs local time 我有一个获取当地时间的输入框

date_default_timezone_set('America/Los_Angeles');
echo "<input type='datetime-local' name='fromDate' class='dates'>";

When I enter 12-31-2014 10:00:00 PM in the input 当我在输入中输入12-31-2014 10:00:00 PM

echo $_POST['fromDate'];

Response: 2014-12-31T22:00:00 回应: 2014-12-31T22:00:00

$test = new DateTime($_POST['fromDate']);
echo $test;

I get 2014-12-31T22:00:00 America/Los_Angeles 我得到2014-12-31T22:00:00 America/Los_Angeles

Then when I convert 然后当我转换

$from_dateGMT  = new DateTime($_POST['fromDate'], new DateTimeZone('Europe/Paris'));
$from_date = $from_dateGMT->format('Y-m-d\TH:i:s');
echo $from_date;

I get 2014-12-31T22:12:00 UTC , which is the same time listed above and should be adding 8 hours. 我得到2014-12-31T22:12:00 UTC ,这是上面列出的相同时间,应该相加8个小时。

What am I doing wrong? 我究竟做错了什么?

I don't deal with dates/times in PHP ever, so this is a learning experience for me. 我从来没有在PHP中处理日期/时间,因此这对我来说是一种学习经验。

Perhaps this will work 也许这会工作

$test = new DateTime($_POST['fromDate'], new DateTimeZone('America/Los_Angeles'));
$test->setTimezone(new DateTimeZone('Europe/Paris'));
echo $test->format('Y-m-d\TH:i:s');

At least that is how it is done in php manual here: http://us2.php.net/manual/en/datetime.settimezone.php 至少这是在php手册中做到的: http : //us2.php.net/manual/en/datetime.settimezone.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM