PHP Calculation time difference calculation article published distance now _php tutorial

Source: Internet
Author: User
Tags diff
I believe in a number of Web sites will see a number of articles published after the article is issued in a few seconds ago or a few months ago sent the document, below I will introduce the use of PHP to calculate the time lag to calculate how long the article publication distance from now.
The code is as follows Copy Code


/**
* Time Difference calculation

*/
function Time2units ($time)
{
$year = Floor ($time/60/60/24/365);
$time-= $year * 60 * 60 * 24 * 365;
$month = Floor ($time/60/60/24/30);
$time-= $month * 60 * 60 * 24 * 30;
$week = Floor ($time/60/60/24/7);
$time-= $week * 60 * 60 * 24 * 7;
$day = Floor ($time/60/60/24);
$time-= $day * 60 * 60 * 24;
$hour = Floor ($time/60/60);
$time-= $hour * 60 * 60;
$minute = Floor ($time/60);
$time-= $minute * 60;
$second = $time;
$elapse = ";

$UNITARR = Array (' Year ' = ' + ' Years ', ' month ' = ' ' month ', ' Week ' = ' ' Week ', ' Day ' = ' ~ ',
' Hours ' = ' hour ', ' minutes ' = ' minute ', ' sec ' = ' second '
);

foreach ($unitArr as $CN = $u)
{
if ($ $u > 0)
{
$elapse = $ $u. $CN;
Break
}
}

return $elapse;
}

$past = 2052345678; Some Timestamp in the past
$now = time (); Current timestamp
$diff = $now-$past;

Echo ' published in '. Time2units ($diff). ' former ';
?>

http://www.bkjia.com/PHPjc/632789.html www.bkjia.com true http://www.bkjia.com/PHPjc/632789.html techarticle I believe in a number of Web sites will see a number of articles published after the article is issued in a few seconds ago or a few months ago sent documents, below I will introduce the use of PHP to calculate the time difference ...

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