如何在PHP中基于当前日期获取日期?

如何在PHP中基于当前日期获取日期?,php,Php,我想用PHP获得4个不同的日期:1天、1周、1个月和3个月。 每个日期都是从今天开始的,所以我们假设今天是2014年11月26日 基于当前日期获取过去的日期 $today = date("Y-m-d"); $one_day = $today - //how do I get yesterday from current day? $one_week = $today - //how do I get one week from current day? $one month = $today

我想用PHP获得4个不同的日期:1天、1周、1个月和3个月。 每个日期都是从今天开始的,所以我们假设今天是2014年11月26日

基于当前日期获取过去的日期

$today = date("Y-m-d");
$one_day = $today - //how do I get yesterday from current day?
$one_week = $today -  //how do I get one week from current day?
$one month = $today - //how do I get one month from current day?
$three_month = $today -  //how do I get three month from current day?
函数非常适合这种方法。 使用类似

echo date( 'Y-m-d', strtotime("-1 day"));
echo date( 'Y-m-d', strtotime("-1 week"));
echo date( 'Y-m-d', strtotime("-1 month"));
echo date( 'Y-m-d', strtotime("-1 year"));
您可以使用该类:


有关更多详细信息,请参阅。

您尝试过什么吗?这里有一半的技巧(请参阅第一个结果)。请参阅中的示例
$date = new DateTime('now');
echo $date->format('Y-m-d');

$date = new DateTime('-1 day');
echo $date->format('Y-m-d');

$date = new DateTime('-1 week');
echo $date->format('Y-m-d');

$date = new DateTime('-1 month');
echo $date->format('Y-m-d');

$date = new DateTime('-3 months');
echo $date->format('Y-m-d');