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语言编写的分布式时间序列化数据库,非常适合对数据(跟随时间变化而变化的数据)的跟踪.监控和分析.在我们的项目 ...
随机推荐
- js 漩涡
What's the ball's orbit if they head for it's next ball. <html> <canvas id="ca"&g ...
- 【原创】解决jquery在ie中不能解析字符串类型xml结构的xml字符串的问题
$.fn.extend({ //此方法解决了ie中jquery不识别非xml的类型的xml字符串的问题 tony tan findX: function (name) { if (this & ...
- python环境搭建-设置PyCharm软件的配色方案和Python解释器
设置PyCharm软件的配色方案 设置Python解释器(用于Python2 or 3 的切换)
- 十天冲刺---Day7
站立式会议 站立式会议内容总结: 燃尽图 照片 两个人编码其实效率挺高的.但是在一些方面,比如说页面UI的编写,会非常吃力,很难达到自己的效果. 由于埋头在编码,所以issues的增加随之停止. 有点 ...
- jQuery自定义插件
jQuery自定义插件 jQuery自定义插件按照功能分类,可以分为三类, 1>封装对象方法的插件,(也就是基于某个DOM元素的jQuery对象,局部的) 2>封装全局函数的插件, ( ...
- 用Okhttp框架登录之后的Cookie设置到webView中(转)
1.webview中设置: [java] view plain copy @SuppressWarnings("deprecation") public void synCooki ...
- spring mvc mybatis 搭建 配置文件信息
参考地址:http://blog.csdn.net/fox_lht/article/details/16952683 shiro集成:http://www.cnblogs.com/miskis/p/5 ...
- request 和response
当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基本协议相当重要, 因为它让我们理解了We ...
- C# 输入ip段生成ip地址
具体代码如下: private void button1_Click(object sender, EventArgs e) { string StartIp = ...
- Echarts3
今天用想在后台首页做个图表,看到highcharts没用喜欢的样式 想起了Echarts,百度了下,发现不一样了呢,原来出了Echarts3 感觉这个比Echarts2好用多了 下载了个精简版,ech ...