2

我可以使用 strtotime +1 个月,但我如何在那天 23:59:59 返回?

例如,现在是2013-06-19 03:28如果我添加 1 个月,那么它将返回2013-07-19 03:28但我需要2013-07-19 23:59

4

2 回答 2

6
date("Y-m-d 23:59:59", strtotime("+1 month"));
于 2013-06-20T07:34:07.567 回答
3

虽然 orangepill 的答案很有用,但我认为这更清楚:

<?php

// You time as Unix timestamp
$now = strtotime('2013-06-19 03:28');

$d = new DateTime();

// Whenever doing date calculations, timezone is important. Set it to "UTC" if you don't care.
$d->setTimeZone(new DateTimeZone('America/Detroit'));

// Initialize date ( - but remember, DateTime can take many other forms of date input)
$d->setTimeStamp($now);

// Increase date by 1 month.
$d->add(new DateInterval('P1M'));

// Overwrite the "clock" part of the date
$d->setTime(23, 59, 59);

// Display the result:
var_dump($d);
于 2013-06-20T07:50:39.460 回答