PHP计算总天数的MySQL通过
我的MySQL结构剩余:PHP计算总天数的MySQL通过
startdate
2012-01-01 04:00:00
enddate
2012-12-05 21:55:00
我的PHP
$startDate=row['startdate'];
$endDate=row['enddate]';
$days="";
$days=date("Y-m-d H:i:s");
$days=($startDate-$endDate);
echo $days;
试试这个简单的一行:
<?php
echo round((strtotime($row['enddate'])-strtotime($row['startdate']))/86400);
?>
你可以在http://php.net/manual/en/function.strtotime.php在PHP手册看为strtotime()
。
辉煌。很喜欢它。 – TheBlackBenzKid 2012-01-06 18:10:23
- 为什么不使用DATE_DIFF,内置,MySQL的功能?
- 如果你想坚持使用PHP:首先在两个日期(转换为unix时间戳)上使用strtotime(),然后减去然后格式化。
我已经说DATE_DIFF不包括时间根据PHP网站。例子?我不需要太多的代码。在这里寻找一个班轮。 – TheBlackBenzKid 2012-01-05 22:09:44
strtotime($ endDate)-strtotime($ startDate))/ 86400 这会给你差异的天数。 – mcmajkel 2012-01-05 22:18:41
您可以使用的strtotime()函数的开始和结束日期转换成秒,减去结束日期的开始日期,然后使用一些数学在秒转换为天,终于圆了与楼层()函数的天数。这是我编写和测试的一些代码。
<?php
$startDate = row['startdate'];
$endDate = row['enddate]';
$seconds_left = (strtotime($endDate) - strtotime($startDate));
$days_left = floor($seconds_left/3600/24);
echo $days_left;
?>
您是否在DST的结束日期但不是开始日期的时区中使用日期进行了试用?由于您正在使用'floor()'(而不是'round()'作为我以前的解决方案,因此可能会比实际时间间隔缩短一天。 – AndersTornkvist 2012-01-05 22:41:32
我知道date_diff的作品,但那还不及时。 – TheBlackBenzKid 2012-01-05 22:03:47