在PHP中如何计算时间差,精确到分? unix时间戳做比较,精确到秒time()函数返回的时间戳如果你的时间格式是"2008-10-20 11:11:11"的话,可以用strtotime()来转换. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 貌似简单,实际上有点复杂的问题,哈哈看看这个<?php $regist1 = "05/12/2006"; $regist2 = "10/05/2007"; list($month1,$day1,$year1) = explode("/",$regist1); list($month2,$day2,$year2) = explode("/",$regist2); $regist1 = mktime(0,0,0,$month1,$day1,$year1); $regist2 = mktime(0,0,0,$month2,$day2,$year2); $time_difference = $regist2-$regist1; echo ("时间差:"); echo date("Y",$time_difference) - 1970; echo ("年"); echo date("m",$time_difference) - 1; echo ("个月"); echo date("d",$time_difference) - 1; echo ("天");?> <?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 $date=date('Y-m-d G:i:s'); print_r($date); $date1 = strtotime($date); echo "<br>"; $date_min=date("Y-m-d G:i:s", mktime(date("G"),date("i")-20,date("s")-10,date("m"),date("d"),date("Y"))); print_r($date_min); $date2 = strtotime($date_min); echo "<br>"; $del = round(($date1-$date2)/60); print_r($del); exit();?>还有一个按照jakey9826 说的用了strtotime这个我测试了如果只是计算出分钟,那没有问题 求助:web service 电信的短信平台 PHP 同时下载多个文件 IE和360安全浏览器不显示flash和视频网站视频了,Firefox可以,怎么弄好? 求一個算法! 有没有人碰到过更新数据库后有数据丢失? 日期范围的查询 php是一门编程语言吗?如何学呀? 字符串检索的问题 关于MSSQL数据提交出错的问题. 企业建立网站,请有能力制作的给予支持!并提出意见与价格! php:将EUC-KR编码的文本文件转换为UTF-8编码格式的文件!急~~~~ PHP如何存储日期数据出现时间差
看看这个<?php
$regist1 = "05/12/2006";
$regist2 = "10/05/2007"; list($month1,$day1,$year1) = explode("/",$regist1);
list($month2,$day2,$year2) = explode("/",$regist2); $regist1 = mktime(0,0,0,$month1,$day1,$year1);
$regist2 = mktime(0,0,0,$month2,$day2,$year2); $time_difference = $regist2-$regist1; echo ("时间差:");
echo date("Y",$time_difference) - 1970;
echo ("年");
echo date("m",$time_difference) - 1;
echo ("个月");
echo date("d",$time_difference) - 1;
echo ("天");
?>
/**
* 时间差计算
*
* @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=date('Y-m-d G:i:s');
print_r($date);
$date1 = strtotime($date);
echo "<br>";
$date_min=date("Y-m-d G:i:s", mktime(date("G"),date("i")-20,date("s")-10,date("m"),date("d"),date("Y")));
print_r($date_min);
$date2 = strtotime($date_min);
echo "<br>";
$del = round(($date1-$date2)/60);
print_r($del);
exit();
?>
还有一个
按照jakey9826 说的用了strtotime
这个我测试了
如果只是计算出分钟,那没有问题