RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2469 | Replies: 8
Reply
Join Date: May 2005
Posts: 230
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Adding x amount of days to a specific date?

  #1  
Nov 1st, 2007
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

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

  #2  
Nov 1st, 2007
>0000-00-00

This is meaningless. At least say something like: yyyy-mm-dd
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: May 2005
Posts: 230
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

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

  #3  
Nov 1st, 2007
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.
Reply With Quote  
Join Date: Aug 2006
Posts: 138
Reputation: php_daemon is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
php_daemon php_daemon is offline Offline
Junior Poster

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

  #4  
Nov 1st, 2007
>$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.
Reply With Quote  
Join Date: Sep 2007
Location: Budapest
Posts: 252
Reputation: fatihpiristine has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 14
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

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

  #5  
Nov 2nd, 2007
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.
Do a favour, leave me alone
Reply With Quote  
Join Date: Aug 2007
Location: Cavite,Philippines
Posts: 508
Reputation: ryan_vietnow is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 68
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

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

  #6  
Nov 6th, 2007
Originally Posted by nathanpacker View 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?


you must use mysql_result to get the date.
  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");
  5.  
"death is the cure of all diseases..."
http://ryantetek.wordpress.com
Reply With Quote  
Join Date: May 2005
Posts: 230
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

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

  #7  
Nov 6th, 2007
Thanks guys, that got it working right.
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: AOlevel1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
AOlevel1 AOlevel1 is offline Offline
Newbie Poster

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

  #8  
Nov 8th, 2007
  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.
Reply With Quote  
Join Date: Sep 2007
Location: Budapest
Posts: 252
Reputation: fatihpiristine has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 14
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

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

  #9  
Nov 8th, 2007
even sql can do this but some ppl hate it you know.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:06 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC