Jump to content

finding time between


doddsey_65

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/217508-finding-time-between/
Share on other sites

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.

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.

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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.