PHP Date Script - Substract 3 months

Status
Not open for further replies.

louie

New Member
Does any of you came across a PHP function to be able to remove 3 months from a specific date.

I need to find records in the DB based on start date YEAR-MONTH-01 minus 3months and end date YEAR-MONTH-31, 30 or 28 minus 3 month depending on the dates given.
 

jmcc

Active Member
Does any of you came across a PHP function to be able to remove 3 months from a specific date.

I need to find records in the DB based on start date YEAR-MONTH-01 minus 3months and end date YEAR-MONTH-31, 30 or 28 minus 3 month depending on the dates given.
Not sure about PHP but I think that the DATE_SUB() function in MySQL will allow you to subtract months. Something like this:
SELECT DATE_SUB('2008-08-01', INTERVAL 3 MONTH);

I haven't used this function recently. It may also be dependent on the version of MySQL. Works in 4.n and 5.n.

Regards...jmcc
 

louie

New Member
I am not sure if that will work with MySQL version but the problem I came across was with the date format I need which unfortunately has to be exact

e.g.
start date 2008-01-01
end date 2008-03-31 or 30 or 28/29 (February)

I could manage the start date easily (already done) as all months starts with 01 but the end date seems to be a pain and I thought I might ask if there is a function already available as I might have to spend few hours to create one.
 

paul

Ninja
PHP:
<?php
$3monthsago = mktime(0, 0, 0, date("m")-3, date("d"), date("y"));
echo "3 months ago was ".date("m/d/y", $3monthsago ); 
?>
 

jmcc

Active Member
I am not sure if that will work with MySQL version but the problem I came across was with the date format I need which unfortunately has to be exact

e.g.
start date 2008-01-01
end date 2008-03-31 or 30 or 28/29 (February)

I could manage the start date easily (already done) as all months starts with 01 but the end date seems to be a pain and I thought I might ask if there is a function already available as I might have to spend few hours to create one.
Perhaps using the DATE_SUB to generate the month and then using a limiter on the DATE function might work or alternatively (in a major kludge) use a number of DATE_SUB queries to generate the limits. Subtracting 3 months gives the lower limit, subtracting two months and then a day gives the upper limit. Very inelegant but it may work.

Regards...jmcc
 

louie

New Member
PHP:
<?php
$3monthsago = mktime(0, 0, 0, date("m")-3, date("d"), date("y"));
echo "3 months ago was ".date("m/d/y", $3monthsago ); 
?>

I was just looking at mktime - just need to run few tests.

I will let you know.
 

louie

New Member
As expected the mktime function gives the wrong date for February... ***k

example: minus 1 month from march

from (original 2008-01-01) 2007-10-01 to (original 2008-03-31) 2008-03-02 (wrong due to 28 or 29 days in Feb)

example minus 3 months from given date

from (original 2008-01-01) 2007-10-01 to (original 2008-03-31) 2007-12-31 (correct)

example minus 4 months from given date

from (original 2008-01-01) 2007-10-01 to (original 2008-03-31) 2007-12-01 (wrong due to 30 days only in Nov.)
 

Tom

Member
If you're just looking to return the month you could change the day value in mktime to 1 or anything <= 28. When doing stuff like this I'd normally give a time midway through the day to avoid day light savings problems as well.

Code:
=date("F",mktime(6, 0, 0, date("m")-3, 1, date("y")));


Edit: Just realised you need the end date of the month. You can use the t value in date to return the number of days in the given month, that should work.
 

paul

Ninja
I've been playing with dates recently. Such fun !

Louie you want to say remove 90 days or do you want to remove 3 months. Are you calculating the months as 30 days, 31 days or whatever number is in it ?
 

louie

New Member
90 days was the easy way out and is probably what I am going to do at the end then compare the days in a month with the day the function gives and replace it if needed to do so.

I need exact dates - last day of the month is compared correctly.
 

paul

Ninja
Louie : Did you do anything fancy in the end ?

Currently I need to write a function that adds 2 months to dates, if it's July 31st, and you add 2 months it should spit out Sept 30th, and not Oct 1st.
 
Status
Not open for further replies.
Top