Jump to content


Photo

How to add a week onto a date...


  • Please log in to reply
1 reply to this topic

#1 lidds

lidds
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 25 May 2006 - 06:24 PM

What I want to be able to do is add 1 week onto an existing date e.g.

$date = "1978-10-15";
// here add code to add 1 week onto the above date

The date format I am using is "Y-m-d" and the date could be any stored date i.e. not the current date

Can anyone help.....

Thanks in advance

Simon

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 May 2006 - 06:55 PM

There are a number of different ways to do this, here are two:
  • Use the strtotime() function:
    <?php
    $date = "1978-10-15";
    $newdate = date('Y-m-d',strtotime($date . ' + 1 week'));
    echo $newdate;
    ?>
  • Convert the date to the number of seconds since 1970-1-1 and add the number of seconds in a week (604,800)
    <?php
    $date_secs = strtotime("1978-10-15");
    $new_date = date_secs + 604800;
    echo date('Y-m-d',$new_date);
    ?>
The above will only work with dates since 1-1-1970 in most cases.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users