Php computing time interval implementation program code

Source: Internet
Author: User
In the article, I have talked a lot about the implementation principles of the program code for implementing the php computing time interval and how to analyze how to do this function. if you need it, you can take a closer look. The following example shows how many seconds ago I posted a post on the forum and analyzed the actual situation...

In the article, I have talked a lot about the implementation principles of the program code for implementing the php computing time interval and how to analyze how to do this function. if you need it, you can take a closer look.

The following example shows how many seconds ago we posted a post on the forum and other functions,

Analysis

Actual time PHP

1 second, 2 "seconds"

______________________

30 seconds = 60

5 minutes = 60*10 // here I think it should be five minutes, not one minute. in my opinion, the minute should be 60*2.

10 minutes = 60*20

30 minutes = 60*60

1 hour = 60*60*2

2.5 hours = 60*60*5


Principle

Use the current time (), minus the new time when you input the information (note that it is a timestamp), and the difference is the number of seconds, which can be converted according to your own needs. For example, if you want to convert it to minutes, divide it by 60.

Instance

The code is as follows:

/**
* Time difference calculation
*
* @ Param Timestamp $ time
* @ Return String Time Elapsed
* @ Author Shelley Shyan
* @ Copyright http://www.phprm.com (Professional PHP Architecture)
*/
Function time2Units ($ time)
{
$ Year = floor ($ time/60/60/24/365 );
$ Time-= $ year * 60x60*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' => 'year', 'month' => 'month', 'week' => 'week ', 'Day' => 'day ',
'Hour '=> 'hour', 'mine' => 'Minute ', 'second' => '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 'posted in'. time2Units ($ diff ';
?>

This is written by a beginner.

The code is as follows:

$ Regist1 = "05/12/2006 ";
$ Regist2 = "10/05/2007 ";

List ($ month1, $ day1, $ year1) = explode ("/", $ regist1 );
List ($ mon2,$ day2, $ year2) = explode ("/", $ regist2 );

$ Regist1 = mktime (0, 0, $ month1, $ day1, $ year1 );
$ Regist2 = mktime (0, 0, $ mon2, $ day2, $ year2 );

$ Time_difference = $ regist2-$ regist1;

Echo ("Time difference :");
Echo date ("Y", $ time_difference)-1970;
Echo ("year ");
Echo date ("m", $ time_difference)-1;
Echo ("months ");
Echo date ("d", $ time_difference)-1;
Echo ("day ");
?>


If you like it, choose it.



Address:

Reprinted at will, but please attach the article address :-)

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.