shijan

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 / 365);
$time -= $year * 60 * 60 * 24 * 365;
$month = floor($time / 60 / 60 / 24 / 30);
$time -= $month * 60 * 60 * 24 * 30;
$week = floor($time / 60 / 60 / 24 / 7);
$time -= $week * 60 * 60 * 24 * 7;
$day = floor($time / 60 / 60 / 24);
$time -= $day * 60 * 60 * 24;
$hour = floor($time / 60 / 60);
$time -= $hour * 60 * 60;
$minute = floor($time / 60);
$time -= $minute * 60;
$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 = 2052345678; // Some timestamp in the past
$now = time(); // Current timestamp
$diff = $now - $past;
echo '发表于' . time2Units($diff) . '前';
?>
<?php
echo "今天:",date('Y-m-d H:i:s'),"<br>";
echo "明天:",date('Y-m-d H:i:s',strtotime('+1 day'));
?>
上一行输出当前时间,下一行输出明天时间
这里+1 day
可以修改参数1为任何想需要的数 day也可以改成year(年),month(月),hour(小时),minute(分),second(秒)
如
date('Y-m-d H:i:s',strtotime("+1 day +1 hour +1 minute");
可以随便自由组合,以达到任意输出时间的目的
注:该方法之针对1970年以后试用,也就是时间戳的适用范围。
php 常用日期相函数[日期加减,两日期之差,日期转换时间截]
下面这些代码是一些常用的日期处理函数了,可以两个时间的日期加减,两日期之差,日期转换时间截等。
echo date('Y-m-d',strtotime('+1 d',strtotime('2009-07-08')));//日期天数相加函数
echo date("Y-m-d",'1246982400');
echo '<br>';
echo date("Y-m-d",'1279123200');
die();
$d = "2009-07-08 10:19:00";
echo date("Y-m-d",strtotime("$d +1 day")); //日期天数相加函数
function dateToTime($d)//把日期转换成时间堆截
{
$year=((int)substr("$d",0,4));//取得年份
$month=((int)substr("$d",5,2));//取得月份
$day=((int)substr("$d",8,2));//取得几号
return mktime(0,0,0,$month,$day,$year);
}
/*
下面函数计算两日期之差
*/
$Date_1="2009-07-08";
echo $Date_1+1;
$Date_2="2009-06-08";
$Date_List_a1=explode("-",$Date_1);
$Date_List_a2=explode("-",$Date_2);
$d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]);
$d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]);
$Days=round(($d1-$d2)/3600/24);
echo "两日期之前相差有$Days 天";
shijan的更多相关文章
随机推荐
- 如何让同一个字段在不同的view中显示不同的内容
many2one 字段默认显示 对象的name字段, 但也可以定义name_get方法显示不同的内容 如res.partner 对象可以根据 context 内容是否显示 客户的地址,职位,email ...
- C# asp:Repeater DataSource List<T>
1. asp:Repeater 数据源为List<T> 2.页面显示 3.行绑定取值
- Win10的革新与突破
7月29号win10全球发布,作为微软最后一款操作系统,它会有那些令人期待的革新,作为一个互联网后现代时代,这款操作系统又会融入什么特别元素? 抱着期待,更新了win10后,我花了大概一天的时间,亲身 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- hadoop 异常 ls: Cannot access .: No such file or directory.
bin/hadoop dfs -lsls: Cannot access .: No such file or directory. bin/hadoop dfs -ls /用这个命令代替试试 原因是格 ...
- 使用 getNextException() 来检索已经过批处理的特定元素的异常。 ERRORCODE=-4228, SQLSTATE=null
今天查询了一天发现的问题,用ibatis做批量操作时,报错: [非原子批处理出现故障]使用 getNextException() 来检索已经过批处理的特定元素的异常. ERRORCODE=-4228, ...
- Qt入门(20)——Qt模块简介
当你安装Qt时,这些模块会被构建到库中.在Qt企业版.Qt评估版和Qt自由版中,包含所有的模块.对于Qt专业版,提供基本的模块--工具.核心.窗口部件.对话框.图标视图和工作区模块.画布模块画布模块提 ...
- hihoCoder 1391 Countries 【预处理+排序+堆】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
#1391 : Countries 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are two antagonistic countries, countr ...
- ActionScript接收socket服务器发送来的数据
原文地址:http://www.asp119.com/news/2009522181815_1.htm 从socket中接收数据的方法取决于你使用socket类型,Socket和XMLSocket都可 ...
- 《JavaScript语言精髓与编程实践》读书笔记二
第3章非函数式语言特性 这一章首先介绍了语言的分类,命令式(结构化编程,面向对象编程),说明式(函数式等).而这一章,主要介绍JS的非函数式特点. 在开始之前,首先介绍了由“结构化编程”向“面向对象编 ...