Php count the number of days between 2 dates when an excess of 1 hour adds another 1 day

im having trouble getting the exact number of days. given that I have a date / time that counts hours in counting the number of days below the code, give me zero days

$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-07 11:10:00");
$days=$fisrstDate->diff($secondDate)->days;

Another example: this should give me 2 days, but it only shows 1 day, when my idea is that after 24 hours I want to add another 1 day so that it gives me a way out in 2 days.

$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-08 05:00:00");
$days=$fisrstDate->diff($secondDate)->days;
+4
source share
1 answer

You can use strtotimeto get exact seconds between two timestamps, and then convert them to days that follow ceilto make it work. For instance:

$fisrstDate = strtotime("2018-03-07 04:46:00");
$secondDate = strtotime("2018-03-07 11:10:00");
$days = abs(ceil((abs($fisrstDate - $secondDate)/ (60 * 60 * 24)) - (1 / 24)));
echo $days;
+1
source

Source: https://habr.com/ru/post/1694887/


All Articles