How to calculate the time comparison and difference between PHP

Source: Internet
Author: User

1.<?PHP 2.$zero 1 =Date ("Y-m-d h:i:S "); 3.$zero 2 = "2010-11-29 21:07:00; 4. echo "Zero1 time is:".  $zero 1. " <br>"; 5. echo "Zero2 time is:".  $zero 2. " <br>"; 6. if (strtotime ($zero 1) <strtotime ($zero 2)) {7. echo "Zero1 earlier than zero2′; 8.}Else{9. echo "Zero2 earlier than zero1′; Ten. } 11. ?>                   

Above is the size of the comparison two absolute time

 <? php  $zero 1=strtotime ( date ("y-m-d h:i:s")); // current time, note h is 24 hours h is 12 hours  $zero 2=strtotime ("2014-1-21 00:00:00"); // New Year time, cannot write 2014-1-21 24:00:00 this does not  $guonian =ceil ( $zero 2- $zero 1)/86400); //60s*60min*24h echo " <strong> $guonian </strong>               

Above is the Countdown applet instance code

<?php//PHP calculates two time-difference methods$startdate = "2010-12-11 11:40:00";$enddate = "2012-12-12 11:45:09";$date =Floor ((Strtotime ($enddate)-Strtotime ($startdate)/86400);$hour =Floor ((Strtotime ($enddate)-Strtotime ($startdate))%86400/3600);$minute =Floor ((Strtotime ($enddate)-Strtotime ( $startdate)) %86400/60);  $second =floor (( Strtotime ( $enddate)-strtotime (  $startdate)) %86400%60); echo  $date. " Day <br> "; echo  $hour. " Hours <br> "; echo  $minute. " Minutes <br> "; echo  $second. " Seconds <br> "
 

<?php
/**
* Time Difference calculation
*
* @param Timestamp $time
* @return String Time Elapsed
* @author Shelley Shyan
* @copyright http://phparch.cn (Professional PHP Architecture)
*/
functionTime2units($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 (Years=' Year ',' A month '=' Month ',Week=' Week ',Days=' Day ',
Hours=' Hour ',Minutes=' Minute ',Seconds=' Second '
);

foreach ($unitArrAs$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). ' former ';? >

How to calculate the time comparison and difference between PHP

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.