如果今天是 2016-02-09减去 3 年(或减去 1095 天)新日期是 2013-02-09现在我需要在 2013-02-09 之前获取去年 1 月 1 日的日期结果 2013-01-01
也许应该是这样的:时间("1095 天前的最后一个 1 月 1 日")或时间("一月一日-3年")
使用 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');
要输出 3 年初的 1 月 1 日日期,然后使用 strtotime now()
,您可以简单地使用以下代码:
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 );