<?php
$zero1=date(“y-m-d h:i:s”);
$zero2=”2010-11-29 21:07:00′;
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)/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计算两个时间差的方法
还有一个简单的方法:
<?php echo floor((time()-strtotime("2014-06-15"))/86400);//86400为天,请根据情况修改 ?>
<?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) . '前';
?>
格式化时间
date() 函数用于格式化时间,返回一个字符串。
语法:
string date( string format [, int timestamp] )
参数 format 表示时间格式化的方式,可能的方式如下:
| 格式化方式 | 说明 |
|---|---|
| Y | 4位数字年,y为2位数字,如99即1999年 |
| m | 数字月份,前面有前导0,如01。n 为无前导0数字月份 |
| F | 月份,完整的文本格式,例如 January 或者 March |
| M | 三个字母缩写表示的月份,例如 Jan 或者 Mar |
| d | 月份中的第几天,前面有前导0,如03。j 为无前导0的天数 |
| w | 星期中的第几天,以数字表示,0表示星期天 |
| z | 年份中的第几天,范围0-366 |
| W | 年份中的第几周,如第32周 |
| H | 24小时格式,有前导0,h为12小时格式 |
| G | 24小时格式,无前导0,g为对应12小时格式 |
| i | 分钟格式,有前导0 |
| s | 秒格式,有前导0 |
| A | 大写上下午,如AM,a为小写 |
可选参数 timestamp 表示时间戳,默认为 time() ,即当前时间戳。
我们可以通过 date() 函数提供的丰富格式化来显示需要的时间日期,如下面的例子:
date("Y-m-d",time()); //显示格式如 2008-12-01
date("Y.m.d",time()); //显示格式如 2008.12.01
date("M d Y",time()); //显示格式如 Dec 01 2008
date("Y-m-d H:i",time()); //显示格式如 2008-12-01 12:01
提示
如果您输出的时间和实际时间差8个小时(假设您采用的北京时区)的话,请检查php.ini文件,做如下设置:
date.timezone = PRC
如需做其他时区的设置请参考:http://www.php.net/manual/en/timezones.php
本文参考:
http://www.cnblogs.com/wellsoho/p/3248082.html
http://www.5idev.com/p-php_time_date.shtml