DateTime“上个月的第一天”不返回第一天

rin*_*t.6 2 php datetime datediff

我已经看过这个答案了,它与我所拥有的非常接近。

这是我的PHP代码:

$start = new DateTime('0:00 first day of previous month', new DateTimeZone('UTC'));
/*
if (isset($_GET['year']) && isset($_GET['month']) && checkdate($_GET['month'], 1, $_GET['year'])) {
    $start = DateTime::createFromFormat('Y-m-d', $_GET['year'] . '-' . $_GET['month'] . '-1');
}*/
$middle = DateTime::createFromFormat('U', strtotime('first day of last month', $start->format('U')));
$middle->setTimezone(new DateTimeZone('UTC'));
$end = DateTime::createFromFormat('U', strtotime('first day of 2 months ago', $start->format('U')));
$end->setTimezone(new DateTimeZone('UTC'));

var_dump($start);
var_dump($middle);
var_dump($end);
Run Code Online (Sandbox Code Playgroud)

今天是 8 月 27 日,所以我预计是 7 月 1 日、6 月 1 日和 5 月 1 日。以下是实际输出:

object(DateTime)[1]
  public 'date' => string '2013-07-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

object(DateTime)[2]
  public 'date' => string '2013-05-02 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

object(DateTime)[3]
  public 'date' => string '2013-04-02 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)
Run Code Online (Sandbox Code Playgroud)

为什么它会在我的月份的第二天返回?

我也试过它没有new DateTimeZone('GMT')作为初始构造函数的第二个参数,DateTime但它仍然给我相同的结果,只是时间不同。

ılǝ*_*ılǝ 6

这部分无关紧要 - 问题已编辑

因为时区不同。$start 在“Rainy River 时区”中计算,而 $middle 和 $end 在 UTC 时间中计算。'Rainy River 时区与 UTC有-06:00 小时偏移(正好是第一个与第二个和第三个结果之间的小时差)。

更新 1 - 解决方案

似乎问题出在 strtotime 附近。出于某种原因,它产生了一个偏移一天的结果(需要进一步解释)。一个简单的解决方案是从该日期减去一秒,它将产生正确的结果。

$timezone = new DateTimeZone('UTC');
$start = new DateTime('0:00 first day of previous month', $timezone );
$middle = DateTime::createFromFormat('U', strtotime('first day of last month',($start  ->format('U'))-1),$timezone);
echo $middle->format('Y-m-d')."\n";
Run Code Online (Sandbox Code Playgroud)

结果:

2013-05-01
Run Code Online (Sandbox Code Playgroud)

更新 2 - 问题原因

最终我发现问题源于第一个日期对象的实例化。这是一个插图。

这将给出正确的结果:

$original = new DateTime('2013-05-01');
echo $original->format('Y-m-d')."\n";

$previous= DateTime::createFromFormat('U', strtotime('first day of last month',($original->format('U'))),new DateTimeZone('UTC'));
echo $previous->format('Y-m-d')."\n";
Run Code Online (Sandbox Code Playgroud)

结果(正常):

2013-05-01
2013-04-01   <--- OK
Run Code Online (Sandbox Code Playgroud)

但是,这不会(只有第一行不同,如原始代码中所示):

$original = new DateTime('0:00 first day of previous month', new DateTimeZone('UTC'));
echo $original->format('Y-m-d')."\n";

$previous= DateTime::createFromFormat('U', strtotime('first day of last month',($original->format('U'))),new DateTimeZone('UTC'));
echo $previous->format('Y-m-d')."\n";
Run Code Online (Sandbox Code Playgroud)

结果:

 2013-07-01
 2013-05-02  <--- BAD
Run Code Online (Sandbox Code Playgroud)