gpt4 book ai didi

php - 计算两个日期之间的夜数

转载 作者:行者123 更新时间:2023-12-04 12:12:15 25 4
gpt4 key购买 nike

我想如何提取确切的夜晚数。我试图通过两种方式实现这一点,但不起作用。它返回 20 晚,但实际上是 21 晚(3 月有 31 天)

$startTimeStamp = strtotime("14-03-2017");                                   
$endTimeStamp = strtotime("04-04-2017");

$timeDiff = abs($endTimeStamp - $startTimeStamp);

$numberDays = $timeDiff/86400; // 86400 seconds in one day


$numberDays = intval($numberDays);

echo $numberDays;

echo floor((strtotime('04-04-2017')-strtotime('14-03-2017'))/(60*60*24));

最佳答案

使用新的 DateTime 扩展 PHP 5.3+:

$date1 = new DateTime("2010-07-06");
$date2 = new DateTime("2010-07-09");

// this calculates the diff between two dates, which is the number of nights
$numberOfNights= $date2->diff($date1)->format("%a");

关于php - 计算两个日期之间的夜数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41183536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com