SkyRanger Posted June 15, 2009 Share Posted June 15, 2009 Does anybody know where I can find a good tutorial for converting dates? What I am trying to do is is have mysql only show entries from a certain time frame. Example: Show all entries from: 15 June 2009 - 5 Days Then one for 10 days then 30 days. Quote Link to comment https://forums.phpfreaks.com/topic/162254-convert-date-then-subtract-days/ Share on other sites More sharing options...
Maq Posted June 15, 2009 Share Posted June 15, 2009 Does anybody know where I can find a good tutorial for converting dates? Assuming you're referring to PHP - date and strtotime. What I am trying to do is is have mysql only show entries from a certain time frame. Are your dates stored in the database as 'DATE' or 'TIMESTAMP'? Quote Link to comment https://forums.phpfreaks.com/topic/162254-convert-date-then-subtract-days/#findComment-856306 Share on other sites More sharing options...
SkyRanger Posted June 15, 2009 Author Share Posted June 15, 2009 No they aren't, they are stored as day month year. Quote Link to comment https://forums.phpfreaks.com/topic/162254-convert-date-then-subtract-days/#findComment-856325 Share on other sites More sharing options...
haku Posted June 15, 2009 Share Posted June 15, 2009 You are better off storing them either as type date or a unix timestamp. The benefit of timestamps is that they are quite versatile, and easy to compare. The benefit of date is that there are a number of mysql (if that's what you are using) date functions that you can use, making it easy to optimize some queries whereas with a timestamp you would need to pull the data from the database and then process it after. Quote Link to comment https://forums.phpfreaks.com/topic/162254-convert-date-then-subtract-days/#findComment-856328 Share on other sites More sharing options...
SkyRanger Posted June 15, 2009 Author Share Posted June 15, 2009 ok, thanks guys for the input. not sure why I never used it this time Quote Link to comment https://forums.phpfreaks.com/topic/162254-convert-date-then-subtract-days/#findComment-856376 Share on other sites More sharing options...
SkyRanger Posted June 15, 2009 Author Share Posted June 15, 2009 ok, got it to subtract the dates but for some reason I cannot get get stuff to show. here is what I have so far: $shownews = mktime(0, 0, 0, date("m"), date("d")-5, date("y")); $getnews = date("m/d/y", $shownews); $mysql = mysql_connect("localhost", "*****", "****"); mysql_select_db("******", $mysql) or die(mysql_error()); $resultsn = mysql_query("SELECT * FROM news where ndate >= $getnews") or die(mysql_error()); while($rowsn = mysql_fetch_array( $resultsn )) { ?> <div style="padding-left:33px;padding-top:20px;"><?php echo $rowsn['ndate']; ?></div> <div style="padding-left:33px;padding-top:10px;padding-right:25px;"><?php echo $rowsn['nstxt']; ?></div> <div style="padding-left:33px;padding-top:10px;padding-right:25px;"><?php $extnews = nl2br($rowsn['nltxt']); echo $extnews; ?></div> <div align="center"><img src="images/line.gif" vspace="7" width="223" height="2"></div> <?php } But for some reason it is showing all of the entries not just the ones for the past 5 days. Quote Link to comment https://forums.phpfreaks.com/topic/162254-convert-date-then-subtract-days/#findComment-856513 Share on other sites More sharing options...
Maq Posted June 15, 2009 Share Posted June 15, 2009 Like we said, you should have your dates as type 'DATE' or 'TIMESTAMP' so you can perform logical operations like this. Quote Link to comment https://forums.phpfreaks.com/topic/162254-convert-date-then-subtract-days/#findComment-856517 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.