How long is the PHP implementation to display the time _php tutorial

Source: Internet
Author: User

How long the PHP implementation has been in the past to show the time


This example describes how PHP displays time in a way that has been used for a long time. Share to everyone for your reference. Specific as follows:

Here is a good way to show how long the past, such as: distance now 10 seconds, distance now 1 days and so on.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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

else if ($time _type = = ' h ') {$ts = $val * 60 * 60;}

else if ($time _type = = ' d ') {$ts = $val * 60 * 60 * 24;}

else if ($time _type = = ' Y ') {$ts = $val * 60 * 60 * 24 * 365;}

else {die (' Unknown time Format given! ');}

if ($check _time < (time ()-$ts)) {return true;}

return false;

}

Examples of Use:

Timestamp to test:

(Could is from an 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
\ n ";

}

else {

print ' The given timestamp: '. Date (' l DS \of F Y h:i:s A ', $time);

Print "-is not older than minutes
\ n ";

}

Short Checks:

if (Time_is_older_than (' 10s ', $time)) {print "is older than seconds
\ n "; }

if (Time_is_older_than (' 200m ', $time)) {print "is older than minutes
\ n "; }

if (Time_is_older_than (' 2h ', $time)) {print "is older than 2 hours
\ n "; }

if (Time_is_older_than (' 4d ', $time)) {print "is older than 4 days
\ n "; }

if (Time_is_older_than (' 1y ', $time)) {print "is older than one year
\ n "; }

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1011839.html www.bkjia.com true http://www.bkjia.com/PHPjc/1011839.html techarticle How long the PHP implementation has been used to display the time This example describes how PHP displays time in a way that has been in the past for a long time. Share to everyone for your reference. Specific as ...

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