Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract 4 months from today's date?

Tags:

I need to declare two dates in "Ymd" format: $toDate and $fromDate.

$toDate represents today's date and $fromDate needs to be 4 months earlier than today.

$toDate = Date('Ymd'); $fromDate = ? 

How do I create $fromDate?

like image 291
Jason94 Avatar asked Nov 12 '10 09:11

Jason94


2 Answers

Use the magic of strtotime:

$fromDate = date("Ymd", strtotime("-4 months")); 
like image 62
Pekka Avatar answered Oct 19 '22 23:10

Pekka


see the code below...

$fourmonthsback = date("Ymd", mktime(0, 0, 0, date("m")-4, date("d"),   date("Y"))); 

OR

$fourmonthsback = mktime(0, 0, 0, date("m")-4, date("d"),   date("Y")); 
like image 36
Prabhu M Avatar answered Oct 19 '22 23:10

Prabhu M