Home  >  Article  >  Backend Development  >  获取指定时间的前一天的日期

获取指定时间的前一天的日期

WBOY
WBOYOriginal
2016-06-06 20:36:342094browse

获取昨天的日期: date('Y-m-d', strtotime('-1 days');

但是如果获取某一天的前一天的日期怎么做呢?

回复内容:

获取昨天的日期: date('Y-m-d', strtotime('-1 days');

但是如果获取某一天的前一天的日期怎么做呢?

<code>$d = new Datetime("2015-03-02 18:00:00");
echo $d->modify("-1 day")->format("Y-m-d H:i:s");
</code>

<code>$date = new DateTime('2015-02-14');
$diff = new DateInterval('P1D');
$date->sub($diff);

echo $date->format('Y-m-d'); // 2015-02-13
</code>

获取的某一天肯定是已知的,直接手动设置成已知时间减去一天的时间就好了吧

<code><?php echo (new DateTime("2014-10-25 -1 days", new DateTimeZone("Asia/Shanghai")))->format("Y/m/d"), "\r\n";

    // or 

    echo date("Y-m-d", strtotime("2014-10-25 -1 days")), "\r\n";
?>
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn