Php 用当天的结果计算日期
我想计算日期中的天数,我使用的示例代码如下Php 用当天的结果计算日期,php,Php,我想计算日期中的天数,我使用的示例代码如下 <?php $birthDate = "12-8-2018"; ( m-d-Y) $birthDate = explode("-", $birthDate); $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y") - $birthDate[
<?php
$birthDate = "12-8-2018"; ( m-d-Y)
$birthDate = explode("-", $birthDate);
$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
? ((date("Y") - $birthDate[2]) - 1)
: (date("Y") - $birthDate[2]));
echo "Age is:" . $age; // OUTPUT is -1
?>
我得到的结果仅以年为单位,如何以天数为单位得到结果,上述代码中的结果示例应为19。解决方案很简单:
<?php
$birthDate = "12-8-2018"; // Input date
$birthDate = strtotime($birthDate); // Convert to unix time
$days = (time() - $birthDate)/(60*60*24); // difference / (60 seconds*60 minutes*24 hours)
?>
可能存在的副本
echo floor($days); // 18.674826388889 -> 18