The PHP implementation shows the time in the way that has elapsed

Source: Internet
Author: User
Tags php programming

The example in this article describes how PHP shows time in a way that has been used for a long time. Share to everyone for your reference. Specifically as follows:

Here in a more readable way to show how long it has been in the past, such as: 10 seconds from now, 1 days away from now and so on.

?

1, 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 function Time_is_older_than ($t, $check _time) {$t = Strtolower ($t); $time _type = substr (preg_replace ('/[^a-z]/', ', $t), 0, 1); $val = Intval (preg_replace ('/[^0-9]/', ', ', $t)); $ts = 0; (s) econds, (m) inutes, (d) ays, (y) ears if ($time _type = = ' s ') {$ts = $val;} else if ($time _type = = ' m ') {$ts = $val * 6 0; else if ($time _type = = ' h ') {$ts = $val *) Else if ($time _type = = ' d ') {$ts = $val * *;} else if ( $time _type = = ' Y ') {$ts = $val * * * 365;} else {die (' Unknown time Format given! ');} if ($check _time < (t IME ()-$ts)) {return true;  //Use Example://timestamp to test://(could is from a database or something else) $time = 1146722922; Long If Check:if (Time_is_older_than (' 30m ', $time)) {print ' The given timestamp: '. Date (' L DS of F Y h:i:s A ', $time) ; Print "-is older than minutes<br/>n"; else {print ' The given timestamp: '. Date (' L DS of F Y h:i:s A ', $time); print "-is not older than minutes<br/>n "; }//Short checks:if (Time_is_older_than (' 10s ', $time)) {print ' is older than seconds<br/>n ';} if (Time_is_olde R_than (' 200m ', $time)) {print ' is older than minutes<br/>n ';} if (Time_is_older_than (' 2h ', $time)] {print ' Is O Lder than 2 Hours<br/>n "; } if (Time_is_older_than (' 4d ', $time)) {print ' is older than 4 Days<br/>n ';} if (Time_is_older_than (' 1y ', $time)) { Print "is older than one year<br/>n"; }

I hope this article will help you with your PHP programming.

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.