defroster Posted August 14, 2012 Share Posted August 14, 2012 Hello, I have a database where each post has a corresponding date/time: DATABASE Post 1. 2012-08-12 18:20:15 Post 2. 2012-08-12 16:30:00 Post 3. 2012-08-11 18:20:15 Post 4. 2012-08-10 13:20:30 When looping through and showing the results I want to put a <br /><br /> when there is a new day , but how do I do this? I can only separate them if the the exact time is same on all posts... if ($row['date'] != $previousloopdate){ echo "<br /><br />"; } Thanks Link to comment https://forums.phpfreaks.com/topic/267082-loop-through-and-enter-when-new-day/ Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 I recommend looking into the strtotime () and date () functions in the PHP manual, should give you exactly what you want. Link to comment https://forums.phpfreaks.com/topic/267082-loop-through-and-enter-when-new-day/#findComment-1369420 Share on other sites More sharing options...
defroster Posted August 14, 2012 Author Share Posted August 14, 2012 Thanks for answer. I have been looking into these but cannot fully grasp it.. is there any way I can use code like this? But instead specify if the date is the same, regardless of time.. if ($row['date'] != $previousloopdate){ echo "<br /><br />"; } Link to comment https://forums.phpfreaks.com/topic/267082-loop-through-and-enter-when-new-day/#findComment-1369421 Share on other sites More sharing options...
scootstah Posted August 14, 2012 Share Posted August 14, 2012 Not tested, but try something like: while($row = mysql_fetch_assoc($result)) { $current = date('d', strtotime($row['date'])); if (isset($previous) && $previous != $current) { echo '<br />'; } $previos = $current; } Link to comment https://forums.phpfreaks.com/topic/267082-loop-through-and-enter-when-new-day/#findComment-1369423 Share on other sites More sharing options...
defroster Posted August 14, 2012 Author Share Posted August 14, 2012 Thanks a million! You are the man Link to comment https://forums.phpfreaks.com/topic/267082-loop-through-and-enter-when-new-day/#findComment-1369425 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.