dachshund Posted January 28, 2011 Share Posted January 28, 2011 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 https://forums.phpfreaks.com/topic/225949-date-since-article-was-posted-ie-yesterday-or-14-hours-ago/ Share on other sites More sharing options...
codefossa Posted January 28, 2011 Share Posted January 28, 2011 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 https://forums.phpfreaks.com/topic/225949-date-since-article-was-posted-ie-yesterday-or-14-hours-ago/#findComment-1166486 Share on other sites More sharing options...
dachshund Posted January 28, 2011 Author Share Posted January 28, 2011 where should i input the datetime stored in the database? which is $rows['date'] Link to comment https://forums.phpfreaks.com/topic/225949-date-since-article-was-posted-ie-yesterday-or-14-hours-ago/#findComment-1166494 Share on other sites More sharing options...
codefossa Posted January 28, 2011 Share Posted January 28, 2011 Store it as a unix timestamp and you would use the $rows['date'] as the timestamp in the function wherever you want it. Link to comment https://forums.phpfreaks.com/topic/225949-date-since-article-was-posted-ie-yesterday-or-14-hours-ago/#findComment-1166496 Share on other sites More sharing options...
dachshund Posted January 28, 2011 Author Share Posted January 28, 2011 sorry, i don't understand Link to comment https://forums.phpfreaks.com/topic/225949-date-since-article-was-posted-ie-yesterday-or-14-hours-ago/#findComment-1166507 Share on other sites More sharing options...
dachshund Posted January 28, 2011 Author Share Posted January 28, 2011 ok sorry, being dumb. it's working but at the moment it's echoing out Date: -1156 Seconds instead of stopping seconds at max 60 Link to comment https://forums.phpfreaks.com/topic/225949-date-since-article-was-posted-ie-yesterday-or-14-hours-ago/#findComment-1166520 Share on other sites More sharing options...
dachshund Posted January 28, 2011 Author Share Posted January 28, 2011 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'; ?> Link to comment https://forums.phpfreaks.com/topic/225949-date-since-article-was-posted-ie-yesterday-or-14-hours-ago/#findComment-1166521 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.