We sometimes encounter more specific needs when doing a site, a message is a few days and days or one months ago, the last time you do an app ran into this week, leaving the code used at that time do not need to run around.
PHP Code:
function Calctime ($time)
{
$now = time (); Current time
$add = Strtotime ($time); Convert time to time stamp
$str = ";
$timeSpan = $now – $add;
$days = Floor ($timeSpan/(24 * 3600 * 1000)); Calculate the difference between days
if ($days < 1) {
$leave 1 = $timeSpan% (24 * 3600 * 1000);
$hours = Floor ($leave 1/(3600 * 1000)); Calculate the number of hours
if ($hours < 1) {
Calculate the difference of minutes
$leave 2 = $leave 1 (3600 * 1000);
$minutes = Floor ($leave 2/(60 * 1000)); The number of milliseconds remaining after calculating the number of hours
if ($minutes <= 5) {
$str = "just";
} else {
$str = $minutes + "Minutes ago";
}
} else {
$str = $hours + "hours ago";
}
} else {
if ($days) {
if ($days < 30) {
$str = $days + "days ago";
} else {
$str = intval ($days/30) + "months ago";
}
} else {
$str = "A long time ago";
}
}
return $str;
}
JS Code:
function Calctime (time) {
var now = new Date ();
var add = new Date (time);
var str = "";
var timeSpan = Now–add;
Calculate the difference in days
var days = Math.floor (TimeSpan/(24 * 3600 * 1000));
if (days < 1) {
Calculate the number of hours
var leave1 = timeSpan% (24 * 3600 * 1000);
Number of milliseconds remaining after counting days
var hours = Math.floor (leave1/(3600 * 1000));
if (Hours < 1) {
Calculate the difference of minutes
var leave2 = leave1% (3600 * 1000);
The number of milliseconds remaining after calculating the number of hours
var minutes = Math.floor (Leave2/(60 * 1000));
if (minutes <= 5) {
str = "just";
} else {
str = minutes + "minutes ago";
}
} else {
str = hours + "hour ago";
}
} else {
if (days) {
if (days < 30) {
str = days + "day before";
} else {
str = parseint (DAYS/30) + "months ago";
}
} else {
str = "A long time ago";
}
}
return str;
}