Php - 从gmdate()日期减去天数

时间:2013-02-19 02:39:21

标签: php date datetime timezone

我想从GMDATE()中减去180天(用于UTC目的)我在PHP脚本中。我刚看了SO,但几乎没有人使用gmdate()。

这就是我所拥有的:

$from_date = gmdate("Y-m-d H:i:s");

有没有一种简单的方法可以从中减去X天数,而不完全使用不同的功能?或者我应该采取另一种方式吗?

2 个答案:

答案 0 :(得分:4)

使用DateTime。使用日期更好,特别是在使用时区时。

$datetime = new DateTime(null, new DateTimeZone('UTC'));
$datetime->modify('-180 days');
echo $datetime->format('Y-m-d H:i:s');

See it in action

<强>参考

答案 1 :(得分:1)

以UTC格式化应该像;

$timezone = new DateTimeZone('UTC');
$datetime = new DateTime('NOW', $timezone);
$datetime->modify('-180 days');
echo $datetime->format('Y-m-d H:i:s');

日期时间:PHP DateTime class

  

public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

时区:PHP DateTimeZone class

  

public __construct ( string $timezone )