PHP:strtototime,如何获得1月1日的最后一个日期然后NOW()减去N天/ yars?

时间:2016-02-09 14:37:42

标签: php strtotime

如果今天是2016-02-09 减去3年(或减去1095天) 新的约会对象是2013-02-09 现在我需要在2013-02-09之前获得1月1日的日期 结果2013-01-01

它可能是这样的: strtotime('最后1月1日1095天前') 要么 strtotime(' 1月1日 - 3年')

2 个答案:

答案 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 );