PHP calculates a function usage instance before a given time _php tips

Source: Internet
Author: User
This article mainly introduces PHP to calculate a given time before the use of the function, the example analysis of PHP computing time skills, very practical value, the need for friends can refer to the next

The example in this article describes the function usage before PHP calculates a given time. Specific as follows:

Given a time here, calculate how long before this time, for example: 2 days ago, 1 years ago

<?phpfunction prettydate ($date) {$time = Strtotime ($date);  $now = time ();  $ago = $now-$time;    if ($ago <) {$when = round ($ago); $s = ($when = = 1)? "    Second ":" Seconds ";  return "$when $s ago";    }elseif ($ago < 3600) {$when = round ($ago/60); $m = ($when = = 1)? "    Minute ":" Minutes ";  return "$when $m ago";    }elseif ($ago >= 3600 && $ago < 86400) {$when = round ($ago/60/60); $h = ($when = = 1)? "    Hour ":" Hours ";  return "$when $h ago";    }elseif ($ago >= 86400 && $ago < 2629743.83) {$when = round ($ago/60/60/24); $d = ($when = = 1)? "    Day ': "Days";  return "$when $d ago";    }elseif ($ago >= 2629743.83 && $ago < 31556926) {$when = round ($ago/60/60/24/30.4375); $m = ($when = = 1)? "    Month ":" Months ";  return "$when $m ago";    }else{$when = round ($ago/60/60/24/365); $y = ($when = = 1)? "    Year ":" Years ";  return "$when $y ago"; }}echo prettydate ("2012-07-22 12:23:45"). " <br/> "; Echo pRettydate ("2010-11-12 22:25:45"). " <br/> "; Echo prettydate (" 2012-01-01 01:00:00 ")." <br/> "; Echo prettydate (" 2001-05-30 00:00:00 ")." <br/> ";

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

Php+mysql+jquery implementation of password retrieval function

PHP implementation loading fonts and saving

PHP recursive traversal for unlimited classification

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.