php比较时间戳与时间差计算函数( 实用)
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比较时间戳与时间差计算函数( 实用)的更多相关文章
- Javascript时间差计算函数代码实例
Javascript时间差计算函数代码实例 <script language="javascript"> Date.prototype.dateDiff = funct ...
- java 时间戳 、时间差计算(秒、分钟、小时、天数、月份、年)
以下代码就是时间差计算(秒.分钟.小时.天数.月份.年) package me.zhengjie; import java.text.ParseException; import java.text. ...
- 随记MySQL的时间差函数(TIMESTAMPDIFF、DATEDIFF)、日期转换计算函数(date_add、day、date_format、str_to_date)
时间差函数(TIMESTAMPDIFF.DATEDIFF) 需要用MySQL计算时间差,使用TIMESTAMPDIFF.DATEDIFF,记录一下实验结果 select datediff(now(), ...
- Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象
1. 时间字符串 --> 时间戳 1) time 模块 timestring = '2016-12-21 10:22:56' print time.mktime(time.strptime(ti ...
- PHP获取毫秒时间戳,利用microtime()函数
PHP获取毫秒时间戳,利用microtime()函数 php本身没有提供返回毫秒数的函数,但提供了一个microtime()函数,借助此函数,可以很容易定义一个返回毫秒数的函数.php的毫秒是没有默认 ...
- [转] node.js如何获取时间戳与时间差
[From] http://www.jb51.net/article/89767.htm Nodejs中获取时间戳的方法有很多种,例如: 1.new Date().getTime() 2.Date. ...
- LinuxUnix time时间戳的处理转换函数
Linux/Unix time时间戳的处理转换函数 linux下的时间函数 我们在编程中可能会经常用到时间,比如取得系统的时间(获取系统的年.月.日.时.分.秒,星期等),或者是隔一段时间去做某事,那 ...
- JS计算两个日期之间的天数,时间差计算
1.日期之间的天数计算 //计算天数差的函数,通用 function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2017-9-25格式 var aDate, ...
- InfluxDB 聚合函数实用案例
InfluxDB 聚合函数实用案例 文章大纲 InfluxDB 简介 InfluxDB是GO语言编写的分布式时间序列化数据库,非常适合对数据(跟随时间变化而变化的数据)的跟踪.监控和分析.在我们的项目 ...
随机推荐
- nodejs实现的简单接口
var http = require('http'); var mysql = require('mysql'); var connection = mysql.createConnection({ ...
- AngularJS指令的详解
指令作为AngularJS中最为重要的部分,所以这个框架本身也是自带了比较多的的指令,但是在开发中,这些指令通常不能满足我们的需要,所以我们也是需要自定义一些指令的.指令是我们用来扩展浏览器能力的技术 ...
- android-之测试框架的使用AndroidTestCase
Android Manifest.xml中所需要添加的配置 <instrumentation android:name="android.test.InstrumentationTes ...
- 网络流最小割 POJ 3469
题意 2个CPU n个任务 给出在第一个 第二个运行时的花费 m 个 a b 不在同一个CPU运行的额外花费 建图 源点 -> n -> 汇点 权 a1 ...
- httpclient提示Cookie rejected: violates RFC 2109: domain must start with a dot
使用httpclient时发生如下告警信息: WARN - HttpMethodBase.processResponseHeaders(1505) | Cookie rejected: "$ ...
- 网络爬虫讲解(附java实现的实例)
本文转载http://blog.csdn.net/luojinping/article/details/6870898 转载过来主要是防止想百度文库一样突然停止运作,导致很多文库丢失而无法找寻 网络蜘 ...
- C#-ade.net-实体类、数据访问类
实体类.数据访问类 是由封装演变而来,使对数据的访问更便捷,使用时只需要调用即可,无需再次编写代码 实体类是按照数据库表的结构封装起来的一个类 首先,新建文件夹 App_Code ,用于存放数据库类等 ...
- MVVM: 通过 x:Bind 实现 MVVM(不用 Command)
背水一战 Windows 10 之 MVVM(Model-View-ViewModel) 通过 x:Bind 实现 MVVM(不用 Command) 示例1.ModelMVVM/Model/Produ ...
- bash中不可以用字符串做数组下标
bash中可以用字符串做数组下标吗例如 test["abc"]=1------解决方案-------------------- 好像是误会,是awk里可以,bash shell里不 ...
- Maven项目加载JAR包
登陆网站找jar包:http://mvnrepository.com/ 1.http://mvnrepository.com/artifact/net.sf.jxls/jxls-core/1.0.6 ...