繁体   English   中英

PHP:strtototime,如何获取早于1月1日的日期早于NOW()减去N天/年?

[英]PHP: strtototime, how to get a date of last 1 january early then NOW() minus N days/yars?

如果今天是2016-02-09减去3年(或1095天),新日期是2013-02-09,现在我需要在2013-02-09结果2013-01-01之前获取最近一月的日期

可能应该是这样的:strtotime(“ 1095年前最后一月1日”)或strtotime(“一月初-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');

要使用strtotime 输出最近3月1日的早3年,然后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 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM