doddsey_65 Posted November 2, 2010 Share Posted November 2, 2010 Im trying to find the time between the last database entry and the current time and then echo it so it says something like '2 minutes ago'. I tried doing: $last_post_gap = strtotime('NOW') - strtotime($topic_info->topic_last_post_time); $last_post_gap = date('i', strtotime($last_post_gap)); $last_post = date('F j, Y', strtotime($topic_info->topic_last_post_time)); if ($last_post <= '- 1 DAY') { $last_post = 'Yesterday at '.date("g:i a", strtotime($topic_info->topic_last_post_time)); } if ($last_post <= '- 1 MINUTE') { $last_post = 'Less Than 1 Minute Ago'; } else { $last_post = $last_post_gap.' Minutes Ago'; } but that just displays 0 minutes ago regardless of the time. Is there anything im missing? Quote Link to comment https://forums.phpfreaks.com/topic/217508-finding-time-between/ Share on other sites More sharing options...
doddsey_65 Posted November 2, 2010 Author Share Posted November 2, 2010 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/217508-finding-time-between/#findComment-1129350 Share on other sites More sharing options...
Airzooka Posted November 2, 2010 Share Posted November 2, 2010 I'm not entirely sure how date-time functions work, but: http://www.php.net/manual/en/datetime.formats.compound.php This page shows that "ii" is used to achieve a [0-5][0-9] format of seconds. Maybe the format string on line 2 should be "ii" instead of "i"? http://www.php.net/manual/en/datetime.formats.relative.php This says "now" is ignored...? Also, the comparisons on line 6 and onward aren't effective. When you evaluate strings against strings, both strings are converted to numbers (which should both convert to 0, in this case). 0 is never less than or equal to 0. Quote Link to comment https://forums.phpfreaks.com/topic/217508-finding-time-between/#findComment-1129372 Share on other sites More sharing options...
Anti-Moronic Posted November 2, 2010 Share Posted November 2, 2010 I agree. You really should manage the calculations using timestamps. $last_post_gap = time() - strtotime($topic_info->topic_last_post_time); // then, 86400 is 1 day, and 60 is of course one minute. echo $last_post_gap; Go from there. Should be far simpler. Quote Link to comment https://forums.phpfreaks.com/topic/217508-finding-time-between/#findComment-1129376 Share on other sites More sharing options...
doddsey_65 Posted November 2, 2010 Author Share Posted November 2, 2010 thanks for the advice, i got it working before i read your post anti-moronic but a slight issue. when the post was made less than 30 minutes ago it shows how many minutes ago it was made. but it has a leading 0 eg 05 minutes ago. how would i replace that 0 or remove it? my code: $last_post = strtotime($topic_info->topic_last_post_time); $today = strtotime('NOW'); $last_post_gap = $today - $last_post; $last_post_gap = date('i',($last_post_gap)); $min = strtotime('- 1 MINUTE'); $normal = strtotime('- 30 MINUTES'); if ($last_post <= $normal) { echo '<p class="last_post_date">'.date('F j, Y g:i a', strtotime($topic_info->topic_last_post_time)); } elseif ($last_post >= $min) { echo '<p class="last_post_date">Less Than 1 Minute Ago'; } else { echo "<p class=\"last_post_date\">{$last_post_gap} minutes ago"; } Quote Link to comment https://forums.phpfreaks.com/topic/217508-finding-time-between/#findComment-1129385 Share on other sites More sharing options...
Airzooka Posted November 2, 2010 Share Posted November 2, 2010 There might be a time format that shows the time without the trailing zero, but you could also do this, too: $minutes = '03'; if (substr($minutes, 0, 1) === '0') { $minutes = substr($minutes, 1, 2); } // $minutes no longer has trailing zeros Quote Link to comment https://forums.phpfreaks.com/topic/217508-finding-time-between/#findComment-1129391 Share on other sites More sharing options...
doddsey_65 Posted November 2, 2010 Author Share Posted November 2, 2010 thanks it works perfectly now there isnt a time format to display without leading 0's so i used your code. Quote Link to comment https://forums.phpfreaks.com/topic/217508-finding-time-between/#findComment-1129394 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.