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

上面是比较两个绝对时间的大小

<?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>"; ?>

<?php
/**
 * 时间差计算
 *
 * @param Timestamp $time
 * @return String Time Elapsed
 * @author Shelley Shyan
 * @copyright http://phparch.cn (Professional PHP Architecture)
 */
function time2Units ($time)
{
   $year   = floor($time / 60 / 60 / 24 / );
   $time  -= $year * 60 * 60 * 24 * ;
   $month  = floor($time / 60 / 60 / 24 / );
   $time  -= $month * 60 * 60 * 24 * ;
   $week   = floor($time / 60 / 60 / 24 / );
   $time  -= $week * 60 * 60 * 24 * ;
   $day    = floor($time / 60 / 60 / );
   $time  -= $day * 60 * 60 * ;
   $hour   = floor($time / 60 / );
   $time  -= $hour * 60 * ;
   $minute = floor($time / );
   $time  -= $minute * ;
   $second = $time;
   $elapse = '';

$unitArr = array('年'  =>'year', '个月'=>'month',  '周'=>'week', '天'=>'day',
                    '小时'=>'hour', '分钟'=>'minute', '秒'=>'second'
                    );

foreach ( $unitArr as $cn => $u )
   {
       if ( $$u > 0 )
       {
           $elapse = $$u . $cn;
           break;
       }
   }

return $elapse;
}

$past = ; // Some timestamp in the past
$now  = time();     // Current timestamp
$diff = $now - $past;

echo '发表于' . time2Units($diff) . '前';
?>

PHP时间比较和时间差如何计算的更多相关文章

  1. Java 中如何计算两个字符串时间之间的时间差?(单位为分钟)

    Java 中如何计算两个字符串时间之间的时间差?(单位为分钟) import java.text.DateFormat; import java.text.ParseException; import ...

  2. jsp页面根据当前时间和定义时间差计算动态倒计时

    jsp页面根据当前时间和定义时间差计算动态倒计时http://www.jb51.net/article/74140.htm    var maxtime =1000*60; //半个小时,按秒计算,自 ...

  3. c和c++在windows下获取时间和计算时间差的方法总结

    c/c++在windows下获取时间和计算时间差的几种方法总结 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double diff ...

  4. 公司手机打卡app时间和百度时间差30秒解决

    问题: 某天发现公司手机打卡app时间和百度时间差30秒解决 分析: nginx        192.168.0.23 外网 : 220.236.7.43 mysql主    192.168.0.2 ...

  5. Shell初学(六)Linux Shell 时间运算以及时间差计算方法

    Linux Shell 时间运算以及时间差计算方法 时间的加减,以及时间差的计算. 1. 时间加减 这里处理方法,是将基础的时间转变为时间戳,然后,需要增加或者改变时间,变成 秒. 如:1990-01 ...

  6. 解决 JPA 插入 MySQL 时间与实际时间差 13 个小时问题

    问题描述 公司使用的阿里云数据库服务器,插入时间与实际时间差 13 个小时 执行 show variables like "%time_zone%"; 结果如下: Variable ...

  7. 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结

    一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...

  8. 关于mysql查询数据库时间和系统时间差

    1. MySQL数据库表中有两个时间的字段,需要计算他们的时间差: (1)datediff函数来表示时间差. 基本语法: DATEDIFF(datepart,startdate,enddate) 说明 ...

  9. golang 时间戳 时间格式化 获取当前时间 timestamp 计算时间差

    获取当前时间 func Now func Now() Time 1 Now returns the current local time. func (Time) UTC func (t Time) ...

随机推荐

  1. AE笔记

    我想做一个好玩的视频,需要用到AE,写点东西锻炼语言能力,视频教程后续录制到时放链接. 一.合成 拖进项目的视频要新建合成才能加以编辑,新建合成很简单,拖拽你的视频向下到一个类似相片的图标上面就新建成 ...

  2. GIS开源软件大全

    3 - F 3map:行星地球项目由3map驱动,这是一个自由软件,由Telstra宽带基金会创建并支持,提供客户端与服务器的能力以在线再现虚拟地球. Amein!:其界面介于ArcMap和UMN M ...

  3. Android Studio Gradle编译项目报错

    Gradle project sync failed Android Studio每次更新版本都会更新Gradle这个插件,但由于长城的问题每次更新都是失败,又是停止在Refreshing Gradl ...

  4. matlab中textread

    今天打算跑下程序,突然发现,真的很烂,不会读入数据,简单的Iris.txt一上午都没读进去,在此对matlab中的textread函数做下总结,textscan函数待续. 本文主要内容引自http:/ ...

  5. linux实践——字符集

    一.ASCII码 首先是看得懂ASCII码表: 二 八 十 十六 缩写/字符 0000 0000 0 0 00 NUL(null) 0000 0001 1 1 01 SOH(start of head ...

  6. 信息安全系统设计基础exp_4

    北京电子科技学院(BESTI) 实     验    报     告 课程:信息安全系统设计基础 班级:1353 姓名:郑伟.吴子怡 学号:20135322.20135313 指导教师: 娄嘉鹏 实验 ...

  7. 启动图实现:UIScrollView+UIPageControl简单实现

    #import "MJViewController.h"#import "RootViewController.h" @interface MJViewCont ...

  8. c# 技巧之 泛型方法

    泛型 指的是编译时不需要指定具体的参数类型,可以在运行时动态地赋予某一种数据类型的机制.  相信很多人对泛型类并不陌生,像Dictionary,List等结构都属于泛型类.有趣的是,一个函数/方法也可 ...

  9. MVC5 + EF6 + Bootstrap3 (9) HtmlHelper用法大全(下)

    文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part2.html 上一节 ...

  10. $self $index $first $last parent() outerParent()

    index5.html <html><head> <title>$self $index $first $last parent() outerParent()&l ...