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.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Jayreis
  • 253
  • 1
  • 7
  • 28

2 Answers2

2

The decimal throws off strtotime() as that is not a valid format it recognizes. The real issue you have is what exactly is half of a month? If you traverse February it gets really dicey.

This is somewhat easier to do using DateTime() and DateInterval() if you specify exactly what half of a month is:

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

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
1

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

amar essa
  • 26
  • 1
  • 1
    Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Jun 24 '22 at 22:06