Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2006
    Posts
    41

    Unanswered: How to add one week to a given date?

    Hi,

    the following for loop will give the date for the next few weeks from todays date.

    Code:
    for ( $counter = 0; $counter < $recurrent; $counter += 1) 
    {
         $day = date('Y-m-d', strtotime("+ $counter weeks"));
    }
    But I need it to give the date for the next few weeks from any given date. I've tried:

    Code:
    for ( $counter = 0; $counter < $recurrent; $counter += 1) 
    {
          $date = $_POST['date']
         $day = date('$date', strtotime("+ $counter weeks"));
    }
    where $date is a date variable in the form yyyy-mm-dd but this doesn't seem to work.

    Could someone give me a hand.

    Cheers

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    This should help
    George
    Home | Blog

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    ...or you could just add the relevant number of seconds to the desired date variable

    so for one week thats 60 * 60 * 24 * 7. ie seconds * minutes * hours * days
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    The following calculates the epoch time each loop and adds the number of days onto it. Thankfully mktime works with date addition, so (when counter = 4) day 19 + (7*4) = 47, is added on to the time.
    Code:
    <?php
    $recurrent = 4;
    for ( $counter = 0; $counter < $recurrent; $counter += 1) 
    {
        date_default_timezone_set('GMT');
        $day = mktime(0,0,0,date('m'),date('d')+(7*$counter),date('Y'));
        echo date('Y-m-d',$day)."<br>";
    }
    ?>

  5. #5
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Basically you need to separate out your variables.
    Code:
    $date = explode('-',$_POST['date']);
    $day = $date[2];
    $month = $date[1];
    $year = $date[0];
    
    $recurrent = 4;
    for ( $counter = 0; $counter < $recurrent; $counter += 1) 
    {
        date_default_timezone_set('GMT');
        echo date('Y-m-d',mktime(0,0,0,$month,$day+(7*$counter),$year));
        echo "<br/>";
    }
    Last edited by aschk; 09-19-07 at 12:52.

  6. #6
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Just to clarify for you why you second attempt wasnt working :

    Assuming that the passed variable $_POST['date'] is in the right format (i.e. yyyy-mm-dd).
    $date = $_POST['date'];

    You were passing it to date
    date($date,....))

    However the first parameter of the date function is the definition of what you want date to return e.g. 'D F y : O' , so giving it '12-05-2005' is moot because it won't understand that format. If you are uncertain about using the date function see the manual (http://uk2.php.net/manual/en/function.date.php).

Posting Permissions

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