#1
  1. No Profile Picture
    jasonc310771
    Guest
    Devshed Newbie (0 - 499 posts)

    How to workout todays date minus 7 days?


    I have looked around but just can not seem to find the answer.

    I have a date in the format say,
    Y-m-j G:i:s
    000-00-00 00:00:00

    and wish to work out the date 7 days before this
    and also one calndar month before todays date to
    say if it is 1 january 2007 then it would show 1 dec 2006
    as long as the date being the 1st is the same the month number is what needs changing to the previous month.

    is there an easy way that this can be done.

    thanks in advance for any help given

    cheer

    Jason
  2. #2
  3. Recovering Intellectual
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2006
    Location
    Orange County, CA
    Posts
    1,306
    Rep Power
    785
    Originally Posted by jasonc310771
    I have looked around but just can not seem to find the answer.

    I have a date in the format say,
    Y-m-j G:i:s
    000-00-00 00:00:00

    and wish to work out the date 7 days before this
    and also one calndar month before todays date to
    say if it is 1 january 2007 then it would show 1 dec 2006
    as long as the date being the 1st is the same the month number is what needs changing to the previous month.

    is there an easy way that this can be done.

    thanks in advance for any help given

    cheer

    Jason
    a posix timestamp is the current date and time in seconds. there are 86400 seconds in one day.

    7 X 86400 .. and there you have it. look into php's date() and mktime() functions.
    Bugs that go away by themselves come back by themselves
    Beware - your loyalty will not be rewarded
  4. #3
  5. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,945
    Rep Power
    898
    Something like this...

    PHP Code:
    $date '2007-01-04 12:34:31' ;

    $oneWeekAgo strtotime '-1 week' strtotime $date ) ) ;
    $oneMonthAgo strtotime '-1 month' strtotime $date ) ) ;

    echo 
    date 'Y-m-j G:i:s' $oneWeekAgo ) . "<br />\n" ;
    echo 
    date 'Y-m-j G:i:s' $oneMonthAgo ) . "<br />\n" 
    Verify and sanitize ALL USER DATA.

    And, to steal a quote from jeremy, "Explain your problem instead of asking how to do what you decided was the solution." Chances are someone on the forums will know a better or more efficient way to do what you're trying to accomplish.

    Avatar: Stolen by me, shown to me by patrick.

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2005
    Posts
    1,586
    Rep Power
    275
    strtotime();


    PHP Code:
    <?php

    $format 
    'Y-m-j G:i:s';

    $date date $format );

    // - 7 days from today

    echo date $formatstrtotime '-7 day' $date ) );

    echo 
    '<br />';

    // - 1 month from today

    echo date $formatstrtotime '-1 month' $date ) );

    ?>
  8. #5
  9. No Profile Picture
    jasonc310771
    Guest
    Devshed Newbie (0 - 499 posts)
    Just a quick note to say thanks.



    Originally Posted by printf
    strtotime();


    PHP Code:
    <?php

    $format 
    'Y-m-j G:i:s';

    $date date $format );

    // - 7 days from today

    echo date $formatstrtotime '-7 day' $date ) );

    echo 
    '<br />';

    // - 1 month from today

    echo date $formatstrtotime '-1 month' $date ) );

    ?>
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    search database with Y/m/d format in time frame


    This is another method if you need to do a search function for your database from a certain number of days ago including all dates up to today. Just insert the $timelimit variable into your mysql_query

    $today = date('Y/m/d');
    $numdays = 10; // set number of days to search for
    $timelimit = "WHERE date = '".$today."' "; // first instance of the string to search for
    for ($x = 1; $x <= $numdays; $x++)
    {
    $date = date_create($today); // reset $date vaiable
    date_sub($date,date_interval_create_from_date_string($x." days")); // subtract increasing number of days
    $daysAgo = date_format($date,"Y/m/d"); // format your date to match how it was saved in database
    $timelimit .= " OR date = '".$daysAgo."'"; // each string to search for
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535
    Look at new DateTime().
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    Yes, this is why the $date must be reset each time if you are using a for or while loop...
    from php.net: "Note that the add() and sub() methods will modify the value of the object you're calling the method on! This is very untypical for a method that returns a value of its own type. You could misunderstand it that the method would return a new instance with the modified value, but in fact it modifies itself! This is undocumented here. (Only a side note on procedural style mentions it, but it obviously does not apply to object oriented style.)"

IMN logo majestic logo threadwatch logo seochat tools logo