If I have a string like this: 2007-01-01 00:00:00
and I want to add one day to it, how do I do that?

I tried:


function sql_date_shift($date, $shift) {
        return date("Y-m-d H:i:s" , strtotime($shift, strtotime($date)));
}

$theDate = '2007-01-01 00:00:00';
$myDate = sql_date_shift($theDate, "+1 day");

But that gave me 1970-01-01 ....

    I copied and pasted your code and it worked fine for me.

      Worked fine in PHP 4.4.4 for me as well.

      In fact, you can even optimize your code:

      function sql_date_shift($date, $shift) { 
              return date("Y-m-d H:i:s" , strtotime($date.$shift)); 
      } 

      No need to use [man]strtotime/man more than once, really.

        I feel dirty using strtotime, as it seems that it's a function designed for parsing human-readable formats. Also, it could get a bit funny about ambiguous dates and read them as American instead of European (for example).

        So I just use a home made function which parses a date format very strictly (using explode() mostly).

        Moreover, time zones can cause weirdness, I try to keep everything in GMT if possible.

        So I have two functions, DbDateTimeToTime() and TimeToDbDateTime() which convert between a unix timestamp and a mysql date time format (Always in GMT, of course).

        If it becomes necessary to display something out of GMT, I still keep it in GMT in the database, just convert for display.

        Adding a day to a unix timestamp is of course easy - just add 606024 seconds.

        Mark

          Write a Reply...