943,712 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7705
  • PHP RSS
Nov 1st, 2007
0

Adding x amount of days to a specific date?

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Nov 1st, 2007
0

Re: Adding x amount of days to a specific date?

>0000-00-00

This is meaningless. At least say something like: yyyy-mm-dd
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 1st, 2007
0

Re: Adding x amount of days to a specific date?

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.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Nov 1st, 2007
0

Re: Adding x amount of days to a specific date?

>$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.
Reputation Points: 13
Solved Threads: 2
Junior Poster
php_daemon is offline Offline
138 posts
since Aug 2006
Nov 2nd, 2007
0

Re: Adding x amount of days to a specific date?

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...
Last edited by fatihpiristine; Nov 2nd, 2007 at 1:00 pm.
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Nov 6th, 2007
0

Re: 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?
you must use mysql_result to get the date.
php Syntax (Toggle Plain Text)
  1. $query="SELECT DATE_ADD('$mydate', INTERVAL 14 DAY)";
  2. $result=mysql_query($query)
  3.  
  4. $next_date=mysql_result($result,0,"*date field on your table");
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Nov 6th, 2007
0

Re: Adding x amount of days to a specific date?

Thanks guys, that got it working right.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Nov 8th, 2007
0

Re: Adding x amount of days to a specific date?

php Syntax (Toggle Plain Text)
  1. <?php
  2. $num_days = 20;
  3. echo date("M-d-Y", mktime(0, 0, 0, m, d, yyyy)+$num_days*60*60*24);
  4. ?>

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
AOlevel1 is offline Offline
5 posts
since Nov 2007
Nov 8th, 2007
0

Re: Adding x amount of days to a specific date?

even sql can do this but some ppl hate it you know.
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Apr 12th, 2010
0

my method in php

1
Last edited by ripraw; Apr 12th, 2010 at 11:57 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ripraw is offline Offline
5 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Display images in dynamic table
Next Thread in PHP Forum Timeline: Im stumped ???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC