php - logic behind displaying time values with seconds and minutes range

Solution:
From the php manual:
<?php
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago
?>
Answer
Solution:
Print the timestamp in the regular way using PHP and use the timeago plugin for jQuery to do the fancy stuff.
Answer
Solution:
You can use something like this
$time = $now - $time;
if ($time < 60) {
$name = "second";
}
elseif ($time < 3600)
{
$time /= 60;
$name = "minute";
}
elseif ($time < 86400)
{
$time /= 3600; //86400;
$name = "hour";
}
elseif ($time < 604800)
{
$time /= 86400; //604800;
$name = "day";
}
elseif ($time < 31536000)
{
$time /= 604800; //31536000;
$name = "week";
}
else
{
$time /= 31536000;
$name = "year";
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: invalid argument supplied for foreach() laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.