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. POJ 3461 Oulipo

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  2. 理解SQL Server中的权限体系(上)----主体

    原文:http://www.cnblogs.com/CareySon/archive/2012/04/10/mssql-security-principal.html 简介 权限两个字,一个权力,一个 ...

  3. 微软职位内部推荐-Sr. SW Engineer for Azure Networking

    微软近期Open的职位: Senior SW Engineer The world is moving to cloud computing. Microsoft is betting Windows ...

  4. mousedos网络批量部署xp

    小时候对这个东西很好奇,不知道什么原理.一直觉得很好玩.现在研究了下,总结如下 软件的操作步骤很讲究,稍微不慎,则就需要重新来过 知识点: 1,掌握诺顿ghost分区为gh文件 2,学会清理至一个干净 ...

  5. TDD开发感悟

    由于公司要实现TDD形式的开发,所以准备了一下,准备在后续的项目中,投入到TDD的怀抱中. 在找一些参考书目的过程中,偶遇<测试驱动开发的艺术>这本书,书中的编码为JAVA派系,但是书的内 ...

  6. [CareerCup] 1.3 Permutation String 字符串的排列

    1.3 Given two strings, write a method to decide if one is a permutation of the other. 这道题给定我们两个字符串,让 ...

  7. ANSI,UTF8等等这些格式

    之前一直在纠结这些格式到底有什么区别,有时候因为格式的问题会让人抓狂. 下面通过实战来分析下: 下面在windows上建立一个txt文档.txt的优势是没有文件头,这样比较好分析. ANSI格式: 可 ...

  8. win8安装mean.io详解

    最近,老大说要安装mean.io,然后……我的win8华丽丽的就上战场了……这期间真是安装得要生要死……最终也终于“不辱使命”成功安装上了…… 废话不多说,进入正题啦 前提当然是电脑有node.环境… ...

  9. PRML读书会第二章 Probability Distributions(贝塔-二项式、狄利克雷-多项式共轭、高斯分布、指数族等)

    主讲人 网络上的尼采 (新浪微博: @Nietzsche_复杂网络机器学习) 网络上的尼采(813394698) 9:11:56 开始吧,先不要发言了,先讲PRML第二章Probability Dis ...

  10. 论Visual Studio和.NET Framework

    今天在工作的时候听到一席谈话感觉有点不可思议,微软真的是把开发人员惯的有点傻了,微软流水线式的产品让很多开发者认定了"唯一",这当然也说明了微软的成功,不扯太多题外话,今天只是简单 ...