PHP Compute time interval Implementation code _php tutorial

Source: Internet
Author: User
Tags echo date
In the article I said a lot about the PHP calculation time interval Implementation of the program code implementation method principle and analysis of how to do this function, there is a need for students to take a closer look at OH.

Below is an example that tells us about how many seconds before the forum has posted posts such as these features,

Analysis

Actual Time PHP

1 sec 2 "SEC"

______________________

30 sec ==60

5 min ==60*10//Here I think it should be five minutes instead of a minute, and personally think a minute's words should be 60*2

10 min ==60*20

30 min ==60*60

1 hours ==60*60*2

2.5 hours ==60*60*5


Principle

Use the current time (), minus the new time you entered the information, (note is the timestamp), and then the difference is the number of seconds, and then according to their own needs to convert AH. For example, if you want to convert the component clock, divide by 60.

Instance

The code is as follows Copy Code

/**
* Time Difference calculation
*
* @param Timestamp $time
* @return String Time Elapsed
* @author Shelley Shyan
* @copyright http://www.bKjia.c0m (Professional PHP Architecture)
*/
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 ';
?>

It was written by a classmate who was just getting started.

The code is as follows Copy Code

$regist 1 = "05/12/2006";
$regist 2 = "10/05/2007";

List ($month 1, $day 1, $year 1) = Explode ("/", $regist 1);
List ($month 2, $day 2, $year 2) = Explode ("/", $regist 2);

$regist 1 = mktime (0,0,0, $month 1, $day 1, $year 1);
$regist 2 = mktime (0,0,0, $month 2, $day 2, $year 2);

$time _difference = $regist 2-$regist 1;

Echo ("Time difference:");
echo Date ("Y", $time _difference)-1970;
Echo ("year");
echo Date ("M", $time _difference)-1;
Echo ("month");
echo Date ("D", $time _difference)-1;
Echo ("Day");
?>


Well, like to choose their own use it.

http://www.bkjia.com/PHPjc/631674.html www.bkjia.com true http://www.bkjia.com/PHPjc/631674.html techarticle in the article I said a lot about the PHP calculation time interval Implementation of the program code implementation method principle and analysis of how to do this function, there is a need for students to take a closer look at OH. Below ...

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