Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- How do I calculating a future date using PHP?


rocknbil - 3:50 pm on Jun 14, 2012 (gmt 0)


If you're using mySQL, don't calculate by PHP, calculate by mySQL using date and time functions [dev.mysql.com]. You'll likely want to do this very same thing in relation to existing records at some point, for example, a download link that expires three days after a purchase. Both examples below.

$query = "select date_add(curdate(), interval 3 day)";
$result = mysql_query($query) or die("Cannot get future date");
$row = mysql_fetch_array($result);
echo $row[0];

$query = "select date_add(purchase_date, interval 3 day) from customer_purchases where customer_id=$cust_id";
$result = mysql_query($query) or die("Cannot get customer's expiration date");
$row = mysql_fetch_array($result);
echo $row[0];

Variables are of course allowed:

// configuration
$expire_after_purchase = 3;
$expire_interval = 'day'; // month, year, day, minute,

$query = "select date_add(purchase_date, interval $expire_after_purchase $expire_interval) from customer_purchases where customer_id=$cust_id";

If you'd like to reformat the output, no problem. No exploding or program functions, wrap the date math in date_format [dev.mysql.com] (same page: )

$query = "select date_format(date_add(purchase_date, interval $expire_after_purchase $expire_interval),"%m/%d/%Y") from customer_purchases where customer_id=$cust_id"; // MM/DD/YYYY


Not copy and paste code, typed on the fly.


Thread source:: http://www.webmasterworld.com/php/4465087.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com