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

<?php
$zero1=date(“y-m-d h:i:s”);
$zero2=”-- ::′;
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)/); //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))/);
$hour=floor((strtotime($enddate)-strtotime($startdate))%/);
$minute=floor((strtotime($enddate)-strtotime($startdate))%/);
$second=floor((strtotime($enddate)-strtotime($startdate))%%);
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 / / / / );
$time -= $year * * * * ;
$month = floor($time / / / / );
$time -= $month * * * * ;
$week = floor($time / / / / );
$time -= $week * * * * ;
$day = floor($time / / / );
$time -= $day * * * ;
$hour = floor($time / / );
$time -= $hour * * ;
$minute = floor($time / );
$time -= $minute * ;
$second = $time;
$elapse = ''; $unitArr = array('年' =>'year', '个月'=>'month', '周'=>'week', '天'=>'day',
'小时'=>'hour', '分钟'=>'minute', '秒'=>'second'
); foreach ( $unitArr as $cn => $u )
{
if ( $$u > )
{
$elapse = $$u . $cn;
break;
}
} return $elapse;
} $past = ; // Some timestamp in the past
$now = time(); // Current timestamp
$diff = $now - $past; echo '发表于' . time2Units($diff) . '前'; 特别注意:<?php echo time2Units(time()-$row['date']).'前'?> 
?>

php比较时间戳与时间差计算函数( 实用)的更多相关文章

  1. Javascript时间差计算函数代码实例

    Javascript时间差计算函数代码实例 <script language="javascript"> Date.prototype.dateDiff = funct ...

  2. java 时间戳 、时间差计算(秒、分钟、小时、天数、月份、年)

    以下代码就是时间差计算(秒.分钟.小时.天数.月份.年) package me.zhengjie; import java.text.ParseException; import java.text. ...

  3. 随记MySQL的时间差函数(TIMESTAMPDIFF、DATEDIFF)、日期转换计算函数(date_add、day、date_format、str_to_date)

    时间差函数(TIMESTAMPDIFF.DATEDIFF) 需要用MySQL计算时间差,使用TIMESTAMPDIFF.DATEDIFF,记录一下实验结果 select datediff(now(), ...

  4. Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象

    1. 时间字符串 --> 时间戳 1) time 模块 timestring = '2016-12-21 10:22:56' print time.mktime(time.strptime(ti ...

  5. PHP获取毫秒时间戳,利用microtime()函数

    PHP获取毫秒时间戳,利用microtime()函数 php本身没有提供返回毫秒数的函数,但提供了一个microtime()函数,借助此函数,可以很容易定义一个返回毫秒数的函数.php的毫秒是没有默认 ...

  6. [转] node.js如何获取时间戳与时间差

    [From] http://www.jb51.net/article/89767.htm Nodejs中获取时间戳的方法有很多种,例如: 1.new Date().getTime()  2.Date. ...

  7. LinuxUnix time时间戳的处理转换函数

    Linux/Unix time时间戳的处理转换函数 linux下的时间函数 我们在编程中可能会经常用到时间,比如取得系统的时间(获取系统的年.月.日.时.分.秒,星期等),或者是隔一段时间去做某事,那 ...

  8. JS计算两个日期之间的天数,时间差计算

    1.日期之间的天数计算 //计算天数差的函数,通用 function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2017-9-25格式 var aDate, ...

  9. InfluxDB 聚合函数实用案例

    InfluxDB 聚合函数实用案例 文章大纲 InfluxDB 简介 InfluxDB是GO语言编写的分布式时间序列化数据库,非常适合对数据(跟随时间变化而变化的数据)的跟踪.监控和分析.在我们的项目 ...

随机推荐

  1. C++成员变量的初始化顺序问题

    问题来源: 由于面试题中,考官出了一道简单的程序输出结果值的题:如下, class A { private: int n1; int n2; public: A():n2(0),n1(n2+2){} ...

  2. oracle去重等基础问题

    --去重查询方法一:根据id select * from sxe where id in(select min(id) from sxe group by username) order by id ...

  3. 使用jsp/servlet简单实现文件上传与下载

    使用JSP/Servlet简单实现文件上传与下载    通过学习黑马jsp教学视频,我学会了使用jsp与servlet简单地实现web的文件的上传与下载,首先感谢黑马.好了,下面来简单了解如何通过使用 ...

  4. 开发错误11:Configuration with name ‘default’ not found

    开发错误11:Configuration with name 'default' not found 今天在导入一个sdkdemoapp3.0项目时,发现project build.gradle 与m ...

  5. NSMutableURLRequest和NSURLConnection用Post方式上传照

    需求:上传一张照片和内容,内容包括:用户编辑的内容.经纬度.地点等等,就是类似发一个说说.这就是一个很平常的功能,实现的方式想必各位能想到很多. 接下来我想用系统的NSURLConnection来实现 ...

  6. 【USACO 2.2】Party Lamps

    四种开关,n盏灯,1:改变所有灯状态,2:改变奇数灯状态,3:改变偶数灯状态,4:改变3k+1灯状态 给你按开关的总次数c和部分灯限制条件(开或关),一开始都是开着的.($c \leq 10000,n ...

  7. 【USACO】Transformations(模拟)

    Transformations A square pattern of size N x N (1 <= N <= 10) black and white square tiles is ...

  8. Mysql 中 iddata1的缩小步骤

    原因就不多说了,,,, 直接上干货. 1. 备份数据库 mysqldump -q -uroot -ppassword --add-drop-table --all-databases >all1 ...

  9. BZOJ4720 [Noip2016]换教室

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  10. crawler:简要了解一下PhantomJS

    有时,我们需要浏览器处理网页,但并不需要浏览,比如生成网页的截图.抓取网页数据等操作.PhantomJS的功能,就是提供一个浏览器环境的命令行接口,你可以把它看作一个“虚拟浏览器”,除了不能浏览,其他 ...