2

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

这是我的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);

今天是 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)

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

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

4

2 回答 2

6

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

因为时区不同。$start 以“Rainy River timezone”计算,而 $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";

结果:

2013-05-01

更新 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";

结果(正常):

2013-05-01
2013-04-01   <--- OK

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

$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";

结果:

 2013-07-01
 2013-05-02  <--- BAD
于 2013-08-28T00:40:30.020 回答
3

在这里阅读答案后,我有了一个更好的主意:

$start = new DateTime('0:00 first day of previous month');
/*
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 = clone $start;
$middle->modify('first day of last month');
$end = clone $start;
$end->modify('first day of 2 months ago');

var_dump($start);
var_dump($middle);
var_dump($end);

输出:

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

object(DateTime)[2]
  public 'date' => string '2013-06-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'America/Rainy_River' (length=19)

object(DateTime)[3]
  public 'date' => string '2013-05-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'America/Rainy_River' (length=19)

此外,我意识到 aDateTimeImmutable将是$start实例的更好选择(这样我就不必克隆其他两个),但我还没有访问 PHP 5.5 的权限。

于 2013-08-28T01:14:31.837 回答