Finding days between 2 unix timestamps in php Finding days between 2 unix timestamps in php unix unix

Finding days between 2 unix timestamps in php


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)) . '<br />';}

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


I think that a quick workaround for this is to subtract the amount of a days worth of seconds from the end_stamp until you get to the start_tag.

//1 day = 86400 seconds

I would build an array of the days to use later.

EDIT (example)

$difference = 86400;$days = array();while ( $start_time < $end_time ){    $days[] = date('M j Y', $end_time);    $end_time -= $difference;}

This should cover any time frame even if its over a bunch of months.


Try this:

while($date_start <= $date_end) {    echo date('M d Y', $date_start) . '<br>';    $date_start = $date_start + 86400;}

Hope this helps !