I'm calling up a particular date from a field in my database using this code (below). I print a result that looks like this Wed May 23, 2007. Let's suppose I wanted to add five days to the result I found in the database so it would now read Mon May 28, 2007. How would I add five days to my result? I know how to do this in mysql for the database , but I can't seem to do it with php to get the desired result. Thanks in advance.

$result = mysql_query("select UNIX_TIMESTAMP(signup_date) from table where whatever='something'") or die (mysql_error());
while ($row = mysql_fetch_array($result)){
$date = $row['UNIX_TIMESTAMP(signup_date)'];

}
echo date("D M d, Y", $date);

    Here is the code:

    $sql = "SELECT UNIX_TIMESTAMP(DATE_ADD(signup_date, INTERVAL 5 DAY)) FROM table WHERE whatever='something'";
    

      Didn't work. I'm getting Wed Dec 31, 1969 as a result

        What is the data type of the signup_date column?

          If signup_date is a DATE column (passing a DATETIME column might work, and if it doesn't, you'd simply need to use the DATE() MySQL function), you may look into using the ADDDATE() MySQL function. Instead of having PHP format it, you could even make MySQL do this as well using the DATE_FORMAT() function (which is similar to PHP's date() function).

            the field I am pulling the info from is in a datetime format. when I use date_add() with mysql it works perfectly, but I need to display my results to the users of the site. I know I'm probably overlooking something very simple and basic

              Maybe try giving it an alias?

              $sql = "SELECT UNIX_TIMESTAMP(DATE_ADD(signup_date, INTERVAL 5 DAY)) AS plus5 FROM table WHERE whatever='something'";
              $result = mysql_query($sql) or die("SQL error ($sql) - " . mysql_error());
              while($row = mysql_fetch_assoc($result))
              {
                 echo date('D M d, Y', $row['plus5']) . "<br />\n";
              }
              

                The last solution works well except I have to add "1" to the final result. Not such a big deal, but I am just trying to understand for future reference, if the date I pull from my database is May 24 and I use the above code and add "2" the final result is May 25, not May 26. Any reason for this? Like I said, no big deal to add "1" as part of the coding, but I would like to understand why.

                Thanks for the help!

                  Nevermind. My mistake. Everything works perfectly. The numbers are not off by one. I goofed. Thank you again for the help!

                    Write a Reply...