954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding x amount of days to a specific date?

I have looked everywhere, I've figured out how to add x amount of days to the current date, or take away x amount of days from the current date. But let's say I have a date stored in my database as "0000-00-00", that is not the current date. How can I take that date and add a certain number of days to it?

I'm using php with a mysql database. I did find one sql command that works when I run it in the sql command line, but not on php. It's SELECT DATE_ADD('$mydate', INTERVAL 14 DAY)

Of course in the command line I put an actual date in the $mydate portion. But in php, it looks like this: $next_date=mysql_query("SELECT DATE_ADD('$mydate', INTERVAL 14 DAY)");

$mydate is just a date in the form 0000-00-00 pulled from somewhere else in my database. But it keeps spitting out "Resource id #8."

What's a simple way to add some days to a given date?

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

>0000-00-00

This is meaningless. At least say something like: yyyy-mm-dd

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Not exactly meaningless, that's the format you have to put in the mysql table to tell it what format you want. Ok, my date format is yyyy-mm-dd.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

>$mydate is just a date in the form 0000-00-00 pulled from somewhere else in my database. But it keeps spitting out "Resource id #8."

That means $mydate is not what you expect. It's probably the result of mysql_query(). Use mysql_fetch_array() or mysql_result() to get the value.

php_daemon
Junior Poster
140 posts since Aug 2006
Reputation Points: 13
Solved Threads: 2
 

remember the code that i gave to increase numbers by one... change to datediff and it will work :)

$AddOneDay = " mycolumn BETWEEN date_format(Now() + INTERVAL 1 DAY,'%e/%c/%Y - %H:%i:%s') AND date_format(Now(),'%e/%c/%Y - %H:%i:%s') ";

how many days you want, change that 1 to any number... :)

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 

I have looked everywhere, I've figured out how to add x amount of days to the current date, or take away x amount of days from the current date. But let's say I have a date stored in my database as "0000-00-00", that is not the current date. How can I take that date and add a certain number of days to it?

I'm using php with a mysql database. I did find one sql command that works when I run it in the sql command line, but not on php. It's SELECT DATE_ADD('$mydate', INTERVAL 14 DAY)

Of course in the command line I put an actual date in the $mydate portion. But in php, it looks like this: $next_date=mysql_query("SELECT DATE_ADD('$mydate', INTERVAL 14 DAY)");

$mydate is just a date in the form 0000-00-00 pulled from somewhere else in my database. But it keeps spitting out "Resource id #8."

What's a simple way to add some days to a given date?

you must use mysql_result to get the date.

$query="SELECT DATE_ADD('$mydate', INTERVAL 14 DAY)";
$result=mysql_query($query)

$next_date=mysql_result($result,0,"*date field on your table");
ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

Thanks guys, that got it working right.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 
<?php
$num_days = 20;
echo date("M-d-Y", mktime(0, 0, 0, m, d, yyyy)+$num_days*60*60*24);
?>


The above is a purely php way of finding the number of days from a certain date. In the date, notice I put m and d and NOT mm and dd. For mktime use 8, not 08 or you will get the wrong date. This is my first post on daniweb, hope everything views OK.

AOlevel1
Newbie Poster
5 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

even sql can do this but some ppl hate it you know.

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 
ripraw
Newbie Poster
5 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You