Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.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/8/mysql/63.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
Datetime php转换和时区使用_Php_Mysql_Date_Datetime - Fatal编程技术网

Datetime php转换和时区使用

Datetime php转换和时区使用,php,mysql,date,datetime,Php,Mysql,Date,Datetime,我正在尝试将utc时间存储的日期转换为另一个时区,但我似乎无法正确获取它。 我有一段时间: date1 = new DateTime('first day of the month'); date1.setTime(0,0,0); // Since using the first day of the month seems return the current time with different date 默认的日期时区是UTC。我想提及的时间是在“欧洲/阿姆斯特丹”时区。

我正在尝试将utc时间存储的日期转换为另一个时区,但我似乎无法正确获取它。 我有一段时间:

   date1 = new DateTime('first day of the month'); 
   date1.setTime(0,0,0); // Since using the first day of the month seems return the current time with different date
默认的日期时区是UTC。我想提及的时间是在“欧洲/阿姆斯特丹”时区。有没有办法让“欧洲/阿姆斯特丹”时区的时间与UTC的每月第一天时间相等?(呃,对不起,我的问题让人困惑……让我举个例子说明一下)。我试图从数据库中查询

   If UTC date time is June 01, 2013. 00:00:00
   I want to get get May 29, 2013 19:55:00.
我试图获得两个不同时区的声明时间之间的差异,以获得我想要的时间,但似乎没有效果:( 我的编辑/澄清:

如果使用此代码:

   $date1 = new DateTime('first day of the month'); 
   $date1.setTime(0,0,0);
   print_r($date1->format('Y-m-d H:i:s'));
我会得到:

   2013-06-01 00:00:00
如果我使用时区:

   $date1->setTimeZone(new DateTimeZone('Europe/Amsterdame'));
   print_r($date1->format('Y-m-d H:i:s'));
我会得到:(这只是一个示例输出):


因为时差。我想要得到的是相反的:我想要得到转换为“UTC”时区时我会得到的日期时间:06-01-2013 00:00:00。所以我的首选输出是:2013-05-29 21:00:00…

要在PHP中设置默认日期,可以在ini文件或PHP文件中进行设置,如下所示:

date_default_timezone_set('Europe/Amsterdam');
然后,要设置日期格式,请参阅以了解格式

在您的情况下,这将是:

date('j M Y' time());

其中j=天,M=月,Y=年。

您可以这样做

$date = new DateTime('2000-01-01 00:00:00', new DateTimeZone('Europe/Amsterdam'));
echo $date->format('Y-m-d H:i:s P') . "\n";

这不是有效的PHP。你试过了吗:?我不知道UTC 6月1日00:00和阿姆斯特丹5月29日19:55有什么关系。@AmalMurali,嗯,我知道它是无效的。它是一个PSUDOCODE..=\u=@DamienOvereem是的。我知道如何使用setTimezone。我想我的问题真的不清楚。让我编辑一下。。
$date = new DateTime('2000-01-01 00:00:00', new DateTimeZone('Europe/Amsterdam'));
echo $date->format('Y-m-d H:i:s P') . "\n";