PHP Time comparison and difference calculation implementation code

Source: Internet
Author: User
This paper mainly introduces the implementation of PHP and time difference calculation method, involving PHP date and time conversion, operation and other related operational skills, the need for friends can refer to, hope to help everyone.

Example 1:


<?php//php Time comparison and difference calculation://(1). Compare the size of the two absolute time header ("content-type:text/html; Charset=utf-8 ");d ate_default_timezone_set (' PRC '), $zero 1=date (" y-m-d h:i:s ");//$zero 1 =" 2010-11-29 21:07:00 "; zero2= "2010-11-29 21:07:00", echo "Zero1 time:". $zero 1. " <br> "; echo" Zero2 time: ". $zero 2." <br> ";//strtotime-The datetime description of any English text to the Unix timestamp if (Strtotime ($zero 1) <strtotime ($zero 2) {echo" Zero1 earlier than Zero2 ";} else if (Strtotime ($zero 1) >strtotime ($zero 2)) {echo "Zero2 earlier than Zero1";} else{echo "Zero2 equals Zero1";} echo "<br/><br/>";? >

Operation Result:


Zero1 time: 2017-07-24 12:18:39zero2 time: 2010-11-29 21:07:00zero2 earlier than Zero1

Example 2:


<?php//(2). Countdown applet $zero1=strtotime (Date ("y-m-d h:i:s")); The current time, note h is 24 hours h is 12 hours $zero2=strtotime ("2018-1-1 00:00:00"); Chinese New Year//float ceil (float $value)//Returns the next integer not less than value, value if there is a fractional part in a bit. $guonian =ceil (($zero 2-$zero 1)/86400); 60s*60min*24hecho "<strong> $guonian </strong> days from the New Year! "Echo" <br/><br/> ";?" >

Operation Result:


It's 161 days before the new year.

Example 3:


<?php//(3). PHP calculates two time difference method $startdate=date ("y-m-d h:i:s"); $enddate = "2017-7-30 18:00:00";//floor-de-rounding//float floor (float $value) Returns the nearest integer less than value, rounding out the fractional part. $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 "Now is the end time". $date. " Day ". $hour." Hours ". $minute." Minutes ". $second." Seconds "; echo" <br/><br/> ";?" >

Operation Result:


Now it's 6 days, 5 hours, 339 minutes, 56 seconds, from the end time.

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.