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. 【jQuery EasyUI系列】使用属性介绍

    1.ValidateBox The validatebox is designed to validate the form input fields.If users enter invalid v ...

  2. 转-Spring单例模式与线程安全

    问题背景 这段时间在做项目的时候,考虑到Spring中的bean默认是单例模式的,那么当多个线程调用同一个bean的时候就会存在线程安全问题.如果是Spring中bean的创建模式为非单例的,也就不存 ...

  3. C++_STL

    容器概念讲解 vector deque

  4. [转]浅谈jQuery EasyUI的属性设置

    原文地址:http://www.easyui.info/archives/1664.html 对jQuery EasyUI有一定了解的话,应该知道基本上每一个组件都有一个"options&q ...

  5. BuiltWith

    BuiltWith网站技术信息查询工具 利用该网站所提供的功能,你可以查询出某网站背后是由哪些技术来支持的, 比如操作系统的类型.所采用的访问量统计服务.采用的发布平台.广告平台.语言框架.聚合功能. ...

  6. AngularJs学习之一使用自定义的过滤器

    script: 参数item是由AngularJs提供的,是应当被过滤的对象集合.而showComplete是我们传入的参数. {{item.action}} 用ng-model创造一个名为showC ...

  7. Cell右滑的动作状态

    //允许cell可以进行编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)index ...

  8. 操蛋的CTex

    我一向是不屑于在windows下用latex的,看起来不伦不类,是geek就不要用windows,图方便就用word而不是latex.但是台式机上的fedora无法上网,那就委屈一下在windows1 ...

  9. 【BZOJ-1042】硬币购物 容斥原理 + 完全背包

    1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1811  Solved: 1057[Submit][Stat ...

  10. 【bzoj1912】 Apio2010—patrol 巡逻

    http://www.lydsy.com/JudgeOnline/problem.php?id=1912 (题目链接) 题意 给出一棵树,要求在树上添加K(1 or 2)条边,添加的边必须经过一次,使 ...