PHP时间比较和时间差如何计算
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时间比较和时间差如何计算的更多相关文章
- Java 中如何计算两个字符串时间之间的时间差?(单位为分钟)
Java 中如何计算两个字符串时间之间的时间差?(单位为分钟) import java.text.DateFormat; import java.text.ParseException; import ...
- jsp页面根据当前时间和定义时间差计算动态倒计时
jsp页面根据当前时间和定义时间差计算动态倒计时http://www.jb51.net/article/74140.htm var maxtime =1000*60; //半个小时,按秒计算,自 ...
- c和c++在windows下获取时间和计算时间差的方法总结
c/c++在windows下获取时间和计算时间差的几种方法总结 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double diff ...
- 公司手机打卡app时间和百度时间差30秒解决
问题: 某天发现公司手机打卡app时间和百度时间差30秒解决 分析: nginx 192.168.0.23 外网 : 220.236.7.43 mysql主 192.168.0.2 ...
- Shell初学(六)Linux Shell 时间运算以及时间差计算方法
Linux Shell 时间运算以及时间差计算方法 时间的加减,以及时间差的计算. 1. 时间加减 这里处理方法,是将基础的时间转变为时间戳,然后,需要增加或者改变时间,变成 秒. 如:1990-01 ...
- 解决 JPA 插入 MySQL 时间与实际时间差 13 个小时问题
问题描述 公司使用的阿里云数据库服务器,插入时间与实际时间差 13 个小时 执行 show variables like "%time_zone%"; 结果如下: Variable ...
- 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结
一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...
- 关于mysql查询数据库时间和系统时间差
1. MySQL数据库表中有两个时间的字段,需要计算他们的时间差: (1)datediff函数来表示时间差. 基本语法: DATEDIFF(datepart,startdate,enddate) 说明 ...
- golang 时间戳 时间格式化 获取当前时间 timestamp 计算时间差
获取当前时间 func Now func Now() Time 1 Now returns the current local time. func (Time) UTC func (t Time) ...
随机推荐
- Spring之AOP
package org.zln.module.test3_aop.interceptor; import org.aspectj.lang.ProceedingJoinPoint; import or ...
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 动态调用webservice,不需要添加Web References
using System; using System.Collections.Generic; using System.Web; using System.Net; using System.IO; ...
- .NET 常见的偏门问题
1.空格 一般情况下," " 的空格可能被过滤掉,在中文输入法中也同样. 有的人会使用2次空格,但是还是无法达到目的. 实现方法:" "的空格,这不是使用2次空 ...
- [资料]常用Windows CMD指令
1. 查找80端口开放情况 netstat -aon|findstr “80″ 2. 用netstat查询端口占用程序的PID,显示列表的最后一列就是占用程序的PID,然后再利用tasklist找到这 ...
- [tools]google神器浏览器下载
google神器下载 这是一款优化了的google浏览器 http://www.ccav1.me/chromegae.html
- Chrome 插件: 起动本地应用 (Native messaging)
Chrome 插件: 起动本地应用 (Native messaging) www.MyException.Cn 网友分享于:2014-08-01 浏览:3次 Chrome 插件: 启动本地应用 ...
- Android -- 使用inBitmap要注意的地方
SDK版本 需要注意的是inBitmap只能在3.0以后使用.2.3上,bitmap的数据是存储在native的内存区域,并不是在Dalvik的内存堆上. 在android3.0开始,系统在Bitma ...
- 20135316王剑桥 linux第二周课实验笔记
Linux中命令格式为: command [options选项] [arguments参数] //中括号代表是可选的,即有些命令不需要选项也不需要参数 ls或ls .显示是当前目录的内容,这里“.”就 ...
- ASP.NET MVC 5 入门教程 (2) 控制器Controller
文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-controller.html 上一节:ASP.NET MVC ...