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!

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.