homepage Welcome to WebmasterWorld Guest from 54.161.214.221
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How do I calculating a future date using PHP?
I need to calcualte a future date based on a date pulled from a MySQL datab
calvinmicklefinger




msg:4465089
 8:13 pm on Jun 13, 2012 (gmt 0)

I have a date variable stored in a MySQL database that is formatted as "2012-06-13." I want to use PHP to calculate three days into the future. I have tried experimenting with functions and formulas I found in the manual, but I seem to be missing something. They all break the code on my page.

Can someone please help me calculate the future date using these two variables?

$KWcurrentDate = "2012-6-13" ;
$KWdateDifference = "3" ;

(Not sure whether the quotes were neded or not, so I put them in.)

Any help would be appreciated.

 

rocknbil




msg:4465516
 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved