簡體   English   中英

獲取三個半月前的日期

[英]Get date from the three and a half months ago

我知道我能做到

$threemonthsago = date("Y-m-d", strtotime("-3 month"));
echo "$threemonthsago";

但是我想在 3.5 個月前而不是三個月前。 所以我試着做

$threemonthsago = date("Y-m-d", strtotime("-3.5 month"));
echo "$threemonthsago";

然而,它似乎並沒有給我正確的日期,它給了我像 9 月這樣的東西,這是自目前 4 月以來不應該的。

小數點會拋出strtotime() ,因為這不是它識別的有效格式。 您遇到的真正問題是半個月到底是多少? 如果你穿越二月,那真的很冒險。

如果您准確指定半個月是多少,則使用DateTime()DateInterval()會更容易一些:

$date = new DateTime();
$new_date = $date->sub(new DateInterval('P3M15D'));
echo $new_date->format('Y-m-d');

演示

echo(strtotime("-105 天"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM