
PHP 里如何进行时间比较?
有两个时间:第一个:2013-1-3123:59第二个:2013-2-100:01这两个时间我要如何比较,才能得出这两个时间相差1天?...
有两个时间:
第一个:2013-1-31 23:59
第二个:2013-2-1 00:01
这两个时间我要如何比较,才能得出这两个时间相差1天? 展开
第一个:2013-1-31 23:59
第二个:2013-2-1 00:01
这两个时间我要如何比较,才能得出这两个时间相差1天? 展开
5个回答
展开全部
php比较时间一般都是变成时间戳去比较的。
使用函数strtotime函数把时间变成时间戳。
比如:
把2015年7月8日和2015年9月30日进行比较。
// 两个时间点相差多少秒钟
$sub = strtotime("2015-07-08") - strtotime("2015-07-08");
展开全部
<?php
$date1 = '2013-1-31 23:59';
$date2 = '2013-2-1 00:01';
$t1 = strtotime($date1);
$t2 = strtotime($date2);
if (date('Y-m-d', $t1) != date('Y-m-d', $t2)) {//如果不在同一天
echo $date1.' 与 '.$date2.' 相差 '.ceil(($t2-$t1)/24/60/60).' 天';
}
?>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//前面的都已经回答了,不过我也写了个方法就贴上来。
function Countday($from,$to)
{
$from=date('d-m-Y',strtotime($from));
$to=date('d-m-Y',strtotime($to));
$nodays=(strtotime($to) - strtotime($from))/ (60 * 60 * 24); //it will count no. of days
return $nodays;
}
function Countday($from,$to)
{
$from=date('d-m-Y',strtotime($from));
$to=date('d-m-Y',strtotime($to));
$nodays=(strtotime($to) - strtotime($from))/ (60 * 60 * 24); //it will count no. of days
return $nodays;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
echo strtotime("2013-2-1 00:01")-strtotime("2013-1-31 23:59");
先转换成日期戳相减得到时间差,相差120秒
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先提取其中的日期部分,然后都按照当天的00:00:00转化成timestamp,再相减和86400判断。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询