acidglitter Posted August 8, 2007 Share Posted August 8, 2007 So I have a certain date in a table (and the type of field is datetime), what code would show what that date would be in 7 days? I'm having each row expire after 7 days, and I want to be able to make a countdown of how many days/hours/minutes are left until it expires. Quote Link to comment Share on other sites More sharing options...
Foser Posted August 8, 2007 Share Posted August 8, 2007 use the date() function and within that, put mktime()... this should help http://us3.php.net/manual/en/function.mktime.php Quote Link to comment Share on other sites More sharing options...
chronister Posted August 8, 2007 Share Posted August 8, 2007 $one_week_from_today=mktime(0,0,0,date('m'),date('d')+7,date('y')); This is the jist of it, it assumes that you want the date not the time. This will give you 12:00:00am 7 days from now, for more help check out the date() function and mktime() function. Working with time and dates in PHP can be confusing at first, but once you get the hang of working with the seconds since epoch format, it is really easy to get dates in the past, present, and future. You may want to look at strtotime() as well. This will convert the datetime format in the db to a seconds since epoch format for ya. Enjoy Quote Link to comment Share on other sites More sharing options...
acidglitter Posted August 8, 2007 Author Share Posted August 8, 2007 Thanks for the replies I get lost trying to do 7 days after the started date.. this is what I have and it doesnt work... $week=$row['started']+mktime(0,0,0,date('m'),date('d')+7,date('y')); Maybe I'm supposed to change the $row['started'] into the date() format first? Quote Link to comment Share on other sites More sharing options...
chronister Posted August 8, 2007 Share Posted August 8, 2007 Try this, <?php $week=strtotime($row['started'])+mktime(0,0,0,date('m'),date('d')+7,date('y')); echo date('h:i:s m/d/Y', $week ); ?> This should give you something like 3:23:47 8/8/2007 as long as I got the date() formatting right. the datetime field format is something like 1:23:56 5-4-2007 ..... I think In order to use this date/time in an equation you have to convert it to the proper time string, which is where strtotime() comes in. It's late and I am tired so I may have messed this up a little, but I am fairly confident that what I wrote will work. Nate Quote Link to comment Share on other sites More sharing options...
acidglitter Posted August 8, 2007 Author Share Posted August 8, 2007 This is the starting date 2007-08-07 08:28:15 And with that code it showed this 02/12/1909 04:59:59 ho hum this is confusing Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 8, 2007 Share Posted August 8, 2007 you could do this in teh query itself (mysql date/time support is better than php's) <?php $qry = "SELECT date_add(`datefield`, INTERVAL 1 WEEK) as `nxtweek` FROM `table` WHERE {conditions}..."; $qry = mysql_query($qry); $qry = mysql_fetch_assoc($qry); $newdate = $qry['nxtweek']; ?> try that and see what happens... Quote Link to comment Share on other sites More sharing options...
acidglitter Posted August 9, 2007 Author Share Posted August 9, 2007 I had to change the interval to 7 DAYs but after that it worked gorgeously Is there a way I can make a countdown showing how many days/hours/minutes until that new date? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 9, 2007 Share Posted August 9, 2007 javscript... http://www.hashemian.com/tools/javascript-countdown.htm just use the results of your query to set the parameters needed for the javascript... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.