PHP时间比较和时间差如何计算

<?php
$zero1=date(“y-m-d h:i:s”);
$zero2=”2010-11-29 21:07:00′;
echo “zero1的时间为:”.$zero1.”<br>”;
echo “zero2的时间为:”.$zero2.”<br>”;
if(strtotime($zero1)<strtotime($zero2)){
echo “zero1早于zero2′;
}else{
echo “zero2早于zero1′;
}
?>


<?php   
$zero1=strtotime (date("y-m-d h:i:s")); //当前时间  ,注意H 是24小时 h是12小时 
$zero2=strtotime ("2014-1-21 00:00:00");  //过年时间,不能写2014-1-21 24:00:00  这样不对 
$guonian=ceil(($zero2-$zero1)/86400); //60s*60min*24h   
echo "离过年还有<strong>$guonian</strong>天!";   
?>  
上面是倒计时小程序 实例代码



<?php
//PHP计算两个时间差的方法 
$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."天<br>";
echo $hour."小时<br>";
echo $minute."分钟<br>";
echo $second."秒<br>";

?>



7 thoughts on “PHP时间比较和时间差如何计算

  1. Howdy! I realize this is kind of off-topic but I had to ask.
    Does building a well-established website like yours require a massive amount work?
    I’m completely new to blogging however I do write in my journal every day.
    I’d like to start a blog so I can share my personal experience and thoughts
    online. Please let me know if you have any kind of recommendations or tips for new aspiring blog owners.
    Thankyou!

  2. Pingback: Google
  3. Hello I am so thrilled I found your blog page, I really found you by accident, while I
    was searching on Digg for something else, Nonetheless I
    am here now and would just like to say many thanks for a incredible post and a all round
    enjoyable blog (I also love the theme/design), I
    don’t have time to go through it all at the moment but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back to read a lot
    more, Please do keep up the fantastic job.

Leave a Reply

Your email address will not be published. Required fields are marked *