+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 12

Thread: PHP Date Script - Substract 3 months

  1. #1
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default PHP Date Script - Substract 3 months

    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.
    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  2. #2
    jmcc's Avatar
    jmcc is offline Wannabe Geek jmcc is just really nice jmcc is just really nice jmcc is just really nice jmcc is just really nice
    Join Date
    Feb 2006
    Posts
    462

    Default

    Quote Originally Posted by louie View Post
    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
    Last edited by jmcc; 24-07-2008 at 01:44 PM. Reason: sqlising

  3. #3
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

    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.
    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  4. #4
    paul's Avatar
    paul is offline ninja SEO paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,277

    Default

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

  5. #5
    jmcc's Avatar
    jmcc is offline Wannabe Geek jmcc is just really nice jmcc is just really nice jmcc is just really nice jmcc is just really nice
    Join Date
    Feb 2006
    Posts
    462

    Default

    Quote Originally Posted by louie View Post
    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

  6. #6
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

    Quote Originally Posted by paul View Post
    PHP Code:
    <?php
    $3monthsago mktime(000date("m")-3date("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.
    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  7. #7
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

    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.)
    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  8. #8
    Tom
    Tom is offline Wannabe Geek Tom is a splendid one to behold Tom is a splendid one to behold Tom is a splendid one to behold Tom is a splendid one to behold Tom is a splendid one to behold Tom is a splendid one to behold Tom is a splendid one to behold
    Join Date
    Jan 2007
    Location
    Kildare, Ireland
    Posts
    245

    Default

    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.
    Last edited by Tom; 24-07-2008 at 03:11 PM.

  9. #9
    paul's Avatar
    paul is offline ninja SEO paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of paul has much to be proud of
    Join Date
    Dec 2006
    Location
    .de
    Posts
    1,277

    Default

    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 ?

  10. #10
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

    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.
    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Similar Threads

  1. Successful Site in 12 Months with Google Alone
    By montyauto in forum Webmaster Articles
    Replies: 16
    Last Post: 28-01-2010, 08:32 AM
  2. Replies: 48
    Last Post: 21-07-2008, 07:07 PM
  3. Fix this lightbox script
    By wheres me jumpa in forum Coding Help
    Replies: 3
    Last Post: 05-06-2008, 11:20 AM
  4. php compare date
    By louie in forum Coding Help
    Replies: 4
    Last Post: 08-12-2006, 10:50 AM
  5. What's this script?
    By blacknight in forum Webmaster Discussion
    Replies: 0
    Last Post: 23-08-2006, 10:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Optimization by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64