0

I know I can do

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

However I want to get 3.5 months ago not three months ago. So I tried to do

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

However it does not seem to give me the correct date its giving me like September something which it should not be since its currently April.

4

1 回答 1

2

小数点被抛出,strtotime()因为这不是它识别的有效格式。您遇到的真正问题是半个月到底是多少?如果你穿越二月,那真的很冒险。

这更容易使用DateTime()DateInterval()如果您准确指定半个月是:

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

演示

于 2014-04-30T16:20:37.523 回答