如果今天是2016-02-09 减去3年(或减去1095天) 新的约会对象是2013-02-09 现在我需要在2013-02-09之前获得1月1日的日期 结果2013-01-01
它可能是这样的: strtotime('最后1月1日1095天前') 要么 strtotime(' 1月1日 - 3年')
答案 0 :(得分:2)
使用DateTime对象:
$threeYearsAgo = (new DateTime())->modify('-3 years');
$januaryFirst = $threeYearsAgo->format('Y-01-01');
或(如果您的PHP版本不支持第一个示例):
$threeYearsAgo = new DateTime();
$threeYearsAgo->modify('-3 years');
$januaryFirst = $threeYearsAgo->format('Y-01-01');
答案 1 :(得分:0)
要输出 1月1日的最后一个日期,然后now()
strtotime ,您只需使用此代码:
echo date( 'Y-01-01', strtotime( "-3 years" ) );
要获取所需值的日期时间戳,您可以使用以下代码:
$date = strtotime( date( 'Y-01-01', strtotime( "-3 years" ) ) );
如果您更愿意使用DateTime
课程,根据需要获取日期,则可以使用以下代码:
$date = new DateTime( '-3 years' );
$date->setDate( $date->format('Y'), 1, 1 );