Jump to content

date since article was posted ie. yesterday, or 14 hours ago


dachshund

Recommended Posts

hi there,

 

basically in my database i have  DATETIME for when an article was posted, using this, I'd like to make it possible for viewers of the article to see when it was posted in a "Posted Today", "Posted Yesterday" or Posted 23 hours ago" format. If it was posted longer ago than yesterday, then I would like it to just be the normal date.

 

I know how to work out if it was posted today, just through:

 

$date = date('j F', strtotime($rows['date']));

$today=date('j F');

 

but not sure how to do the hours.

 

thanks!

 

function timePassed($pastTimestamp)
{
$currentTimestamp = time();
$timePassed = $currentTimestamp - $pastTimestamp;
$elapsedString = "";
if($timePassed > 604800)
{
	$weeks = floor($timePassed / 604800);
	$timePassed -= $weeks * 604800;
	$elapsedString = $weeks > 1 ? $weeks." Weeks, " : $weeks." Week, ";
}
if($timePassed > 86400)
{
	$days = floor($timePassed / 86400);
	$timePassed -= $days * 86400;
	$elapsedString .= $days > 1 ? $days." Days, " : $days." Day, ";
}
if($timePassed > 3600)
{
	$hours = floor($timePassed / 3600);
	$timePassed -= $hours * 3600;
	$elapsedString .= $hours > 1 ? $hours." Hours, " : $hours." Hour, ";
}
if($timePassed > 60)
{
	$minutes = floor($timePassed / 60);
	$timePassed -= $minutes * 60;
	$elapsedString .= $minutes > 1 ? $minutes." Minutes, " : $minutes." Minute, ";
}
$elapsedString .= $timePassed != 1 ? $timePassed." Seconds" : $timePassed." Second";

return $elapsedString;
}

oh and also, the seconds are getting less and less, as though it's counting down to something, when it should be going up.

 

this is what i echoed out:

 

Posted: <?php $dateNew = strtotime($rows['date']);

echo timePassed($dateNew);

echo ' 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.