#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    essex UK
    Posts
    192
    Rep Power
    14

    Changing date from date ("Ymd");


    I have a start date called by

    PHP Code:
    date ("Ymd"); 
    I need to add a year to get an expiry date.

    I can't seem to get my head around it?

    I also need to store the new date minus 1 month (and take account of the last day of the month ( say date is 2005 03 31 it would need to be 2005 02 28 )

    All help appreciated
    I love perl
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    63
    Rep Power
    14
    Look at PHP's mktime() function. You can do some math in there to get what you need I beleive.

    For instance...
    PHP Code:
    //get a date based on month, day and year
    $dt mktime$m $d $y );

    //get a date 1 year from m, d, and y
    $dt mktime$m $d $y );

    //get the last day of the previous month
    $dt mktime$m $y ); 
    Once you have the $dt in the timestamp you need, then just use the date function to get the value

    PHP Code:
    $dayOfWeek date"w" $dt );
    $numDaysInMonth date"t" $dt ); 
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    essex UK
    Posts
    192
    Rep Power
    14

    thread done


    I tried that but found an easier way now

    PHP Code:
    $todaysDate date("Ymd");
    $advanced_date date("Ymd",time()+366*24*3600);
    $invoice_dt date("Ymd",time()+335*24*3600) ; 
    This gives me 1 year from now, and 11 months from now

    But thanks for your efforts.
    I love perl
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    63
    Rep Power
    14
    Cool, yeah I did it that way before as well actually. But I found it easier to read by using y + 1 as opposed to time()+366*24*3600. I ran into issues when I needed something say the last Wednesday of the month, so I started using the mktime() function. But as the saying goes, there are 100 ways to skin a cat.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Brisbane. AUS
    Posts
    241
    Rep Power
    16
    I was wondering how would I do the above except when that date is not today but coming from a database?

    e.g
    PHP Code:
    $thedate $member->start_date;  // formatted like 0000-00-00
    $one_month_ago date("______",time()-28*24*3600);  
    ?> 
    ???

    Thanx
  10. #6
  11. Never give up
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Sep 2005
    Location
    College Station, TX, USA
    Posts
    2,622
    Rep Power
    812
    If you want to just pull the new value out of the db, you can use the mysql function ADDDATE like this
    Code:
    SELECT
        ADDDATE( your_date_field, INTERVAL '1-11' YEAR_MONTH) AS new_date
    FROM
        your_table;
    And if you want to use it in the date function for formatting, then you should select it as a unix timestamp by using
    Code:
    SELECT
        UNIX_TIMESTAMP(ADDDATE( your_date_field, INTERVAL '1-11' YEAR_MONTH)) AS new_date
    FROM
        your_table;
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Brisbane. AUS
    Posts
    241
    Rep Power
    16
    Ok thanks, once I have selected teh date from the DB and taken 4 weeks off I then use it in the following SQL

    Code:
    SELECT * FROM thelist WHERE start_date > '$thedate' ORDER BY start_date
  14. #8
  15. Never give up
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Sep 2005
    Location
    College Station, TX, USA
    Posts
    2,622
    Rep Power
    812
    If that is all you do with it, then why bother going through all of that?

    Code:
    SELECT
        *
    FROM
        thelist
    WHERE
        start_date > SUBDATE( special_date, INTERVAL 14 DAY )
    ORDER BY 
        start_date
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Brisbane. AUS
    Posts
    241
    Rep Power
    16
    ok, so special_date is the date I am checking agaist yes? The thing is that comes from a different SQL selection earlier on in the coding.

    I tried the following:

    Code:
    SELECT * FROM thelist WHERE start_date > SUBDATE( $thedate, INTERVAL 14 DAY ) ORDER BY start_date
    but this just returned a foreach error..
  18. #10
  19. Never give up
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Sep 2005
    Location
    College Station, TX, USA
    Posts
    2,622
    Rep Power
    812
    Is $thedate in 'YYYY-MM-DD' form?

    If you aren't using it elsewhere, then you can use a subselect to get the date.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Brisbane. AUS
    Posts
    241
    Rep Power
    16
    yes the $thedate is formatted as 'YYYY-MM-DD'
  22. #12
  23. Never give up
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Sep 2005
    Location
    College Station, TX, USA
    Posts
    2,622
    Rep Power
    812
    then use the query like this
    PHP Code:
    $sql "SELECT * FROM thelist WHERE start_date > SUBDATE( '$thedate', INTERVAL 14 DAY ) ORDER BY start_date"
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Brisbane. AUS
    Posts
    241
    Rep Power
    16
    Thankyou very much, this got it!
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    essex UK
    Posts
    192
    Rep Power
    14
    Wow ! to think I started this thread in Jan 2004, I wonder if it is the longest lasting thread?

    Seriously in 2 years and 8 months, I have learnt so much PHP and am now building database driven sites. However, a couple of months ago I had to go back to flat files and PHP so the usefulness of small bits does help!

    Since, I have used Dev shed many times (both to ask and in more recent times to help) So thank you Dev Shed!
    I love perl

IMN logo majestic logo threadwatch logo seochat tools logo