Calculation and current time lag how many days (js/php)

Source: Internet
Author: User
Tags current time php code

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.