I am trying to add 1 to my date variable:

$day2 = date("Y/m/d");

how do I add/subtract 1 from $day2. If a user enters 2010/05/02, I would like the date to be 2010/05/03

Thanks for you help.

    One way might be to use [man]strtotime/man and append "+1 day" to the date string. If a database is involved, there is similar syntax you can use in the query itself rather than using PHP.

      ok, I'm trying to add a day to my date variable using the strtotime(), but it's not working:

      $day2 = mktime(0, 0, 0, date("m") , date("d"), date("Y"));
      $day2 = date("Y-m-d", $day2);
      $day2 = date("Y/m/d");
      $newDate = date("Y/m/d", strtotime("$day2 +1 day days"));

      before the last line, the date I have is "2010/05/27".

      then, when I try to add a day in the last line, I get:

      1969/12/31

      Can someone help?

        8 days later

        You might appreciate the Pear :: Date package:

        http://pear.php.net/package/Date/

        It's actually pretty easy to use. Example:

        <?php
        require_once "Date.php";
        $date1 = new Date();
        echo "current date is: " . $date1->format('%Y/%m/%d') ."\n";
        $date1->addSeconds(86400); // seconds per day
        echo "current date is: " . $date1->format('%Y/%m/%d') ."\n";
        ?>
        
          Tonsil wrote:

          $date1->addSeconds(86400); // seconds per day

          ... except that not every day is 86400 seconds long.

            <?
            $startDate = "2010-06-02";
            $nextDate = strtotime($startDate. "+2 week");
            $format = '%Y-%m-%d';
            echo strftime($format,$nextDate);
            ?>

            -Chad
            www.ChadAyers.org

              bradgrafelman;10954461 wrote:

              ... except that not every day is 86400 seconds long.

              True, if you're not dealing in GMT. That's why it's best to do all your date math in GMT, and only convert to local time at the end. The newest Date package has an addDays method, but it's not always available.

                I find that mktime is way more helpful when trying to add and subtract from dates. It handles leap years and ends of months much better then anything else I had done. It ends up being a big long line of code, but it's really the easiest. I found strtotime and I sometimes don't see eye to eye on what strings should be interpreted as time.

                //first I get the stamp, like I want to take today as my end date
                //then set my start date as this date last year, minus X days.
                //first get my end stamp, this is todays
                $end_stamp = strtotime('now');
                
                //then I use mktime to create a timestamp, I use the date function with my source time stamp
                //($end_stamp) to get each argument mktime requires in the proper format.
                //where I set the days, I've added +$days after I do date('d', $end_stamp);
                //if days is 90, this properly adjusts the timestamp.  Lastly I have a -1 on the year
                //argument, same as the $days, but this is static.
                $start_stamp = mktime(0,0,0, date('m', $end_stamp), date('d', $end_stamp)+$days, date('Y', $end_stamp)-1);
                
                //Now I can format it however I need, as it's a timestamp.
                $start_date = date('Y-m-d', $start_stamp);
                
                

                This is the easiest solution I've found that can deal with rolling over to the next/previous day,month,year, etc.

                http://www.phpbuilder.com/manual/function.mktime.php

                  Or you can simply do

                  $date1 = "2010/05/02";
                  $date2 = date("Y/m/d", strtotime($date1 . "+1 day"));
                  

                  and save yourself the headache and typing. and for future reference, i just figured i would show the syntax in case you need to alter several items.
                  lets say your adding 3 years, 2 months, 12 days, 8 hours, 10 minutes and 30 seconds:

                  $date1 = "2010/05/02 8:10:20";
                  $date2 = date("Y/m/d H:i:s", strtotime($date1 . "+3 year 2 month 12 day 8 hour 10 minute 30 second"));
                  

                  would change the time to "2013/07/14 16:20:50"

                    Write a Reply...