我想用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?
答案 0 :(得分:1)
strtotime() - 函数非常适合这种方法。 使用像
这样的东西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"));
答案 1 :(得分:1)
您可以使用DateTime
类:
$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');
有关详细信息,请参阅Supported Date and Time Formats。