Finding days between 2 unix timestamps in php

前端 未结 8 1188
一整个雨季
一整个雨季 2020-12-13 18:57

Hay, i have a database holding events. There are 2 fields \'start\' and \'end\', these contain timestamps. When an admin enters these dates, they only have the ability to se

8条回答
  •  没有蜡笔的小新
    2020-12-13 19:13

    You just have to calculate the number of seconds between the two dates, then divide to get days :

    $numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
    

    Then, you can use a for loop to retrieve the dates :

    $numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
    
    for ($i = 1; $i < $numDays; $i++) {
        echo date('Y m d', strtotime("+{$i} day", $smallestTimestamp)) . '
    '; }

    Again, if you don't know which timestamp is the smallest, you can use the min() function (second argument in strtotime).

提交回复
热议问题