簡體   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