PHP calculation of the remaining time of the custom function _php Tutorial

Source: Internet
Author: User
The remaining time is when an article is published to now for a few minutes or a few days, this in many blog forums to see the article published 1 days ago, below I introduce two instances

Convert two date-formatted strings to UNIX timestamps, then subtract to get timestamp difference. To determine the time remaining, generate a format like this (2 hours, 30 minutes, 20 seconds ago)

code as follows copy code

Public function gettime ($time _s, $time _ N) {
$time _s = strtotime ($time _s);
$time _n = strtotime ($time _n);
$strtime = ';
$time = $time _n-$time _s;
if ($time >= 86400) {
Return $strtime = Date (' y-m-d h:i:s ', $time _s);
}
if ($time >= 3600) {
$strtime. = Intval ($time/3600). ' Hours ';
$time = $time% 3600;
}else{
$strtime. = ';
}
if ($time >=) {
$strtime. = Intval ($time/60). ' Minutes ';
$time = $time% 60;
}else{
$strtime. = ';
}
if ($time > 0) {
$strtime. = Intval ($time). ' Seconds ago ';
}else{
$strtime = "Time error";
}
Return $strtime;
}

First judge if you want to reduce the value is greater than the number of seconds in a day 86,400 seconds, if it is greater than the original database query time

Then determine if it is within 1 hours to a day, that is, 3,600 seconds-86,400 seconds, and if so, return x hours. After obtaining the result, we need to use the remainder method to remove the time of the hourly part, and use the% to take the surplus

Then determine if it is within 1 minutes to an hour, that is, 60 seconds-3,600 seconds, and if so, return x minutes. After obtaining the result, we need to use the remainder method to remove the minute part time, and use the% take-over

Finally determine whether within 1 minutes, that is, 0 seconds-60 seconds, if it is within the words will return x seconds

Note: The results above are used. = Connected. This will finally get a whole time.

http://www.bkjia.com/PHPjc/632724.html www.bkjia.com true http://www.bkjia.com/PHPjc/632724.html techarticle The remaining time is when an article is published to now have a few minutes or a few days, this in many blog forum to see the article 1 days ago released, below I introduce two instances of two ...

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