Note that there are some explanatory texts on larger screens.

plurals
  1. POActual days between two unix timestamps in PHP
    text
    copied!<p>No this is not the standard +86400 seconds between dates.</p> <pre><code>$start_time = strtotime("2012-01-15 23:59"); $end_time = strtotime("2012-01-16 00:05"); </code></pre> <p>$daysInBetweenTimestamps = ?</p> <p>That is the problem I'm currently facing as the timestamps may range in between a 5 minute to 5 hour time span for instance, using standard +86400 to see if it's more than a day would not work, and due to massive amount of indexing that I'm doing I would like to see if there is a more efficient way to check if a new day has started instead of doing a date("d") > $prevDay on the second level.</p> <p>Updating with the test for the first example: </p> <pre><code>echo "Absolute Start: ".date("Y-m-d H:i:s",$start)."&lt;br /&gt;"; echo "Absolute End: ".date("Y-m-d H:i:s",$end)."&lt;br /&gt;"; echo "Interval Used: $interval(seconds) OR ".($interval / 60)."(minutes)&lt;br /&gt;"; $numberOfIntervals = ceil(($end - $start) / $interval); echo "Number of intervals:$numberOfIntervals&lt;br /&gt;&lt;br /&gt;"; if ($numberOfIntervals &gt; 0){ for ($i = 0; $i &lt; $numberOfIntervals; $i++){ $curStart = $start + ($interval * $i); $curEnd = $curStart + $interval; if ($curEnd &gt; $end){$curEnd = $end;} echo "Interval Start DateTime: ".date("Y-m-d H:i:s",$curStart)."&lt;br /&gt;"; echo "Interval End DateTime: ".date("Y-m-d H:i:s",$curEnd)."&lt;br /&gt;"; /* EXAMPLE PHP5.3 DateTime START - NOT WORKING */ $startDiff = new DateTime("@$curStart"); $endDiff = new DateTime("@$curEnd"); $diff = $startDiff-&gt;diff($endDiff); echo $diff-&gt;format("%a") . " days&lt;br /&gt;"; if ($diff-&gt;format("%a") &gt; 0){ /* EXAMPLE PHP5.3 DateTime END */ /* EXAMPLE Julian START - WORKS */ $days = unixtojd($curEnd) - unixtojd($curStart); echo "Number of days:$days&lt;br /&gt;"; if ($days &gt; 0){ /* EXAMPLE Julian END */ // Multiple days so the log files are split echo "Multiple days so log files are split&lt;br /&gt;"; }else{ echo "Single day so log files are NOT split&lt;br /&gt;"; } } } </code></pre> <p>Output looks as follows:</p> <pre><code>Absolute Start: 2012-01-25 23:59:00 Absolute End: 2012-01-26 00:02:00 Interval Used: 180(seconds) OR 3(minutes) Number of intervals:1 Interval Start DateTime: 2012-01-25 23:59:00 Interval End DateTime: 2012-01-26 00:02:00 </code></pre> <p><strong>=== EXAMPLE 1 START ===</strong></p> <pre><code>0 days Single day so log files are NOT split </code></pre> <p>Am I just missing something on the diff?</p> <p><strong>=== EXAMPLE 1 END ===</strong></p> <p><strong>=== EXAMPLE 3 START ===</strong></p> <pre><code>Number of days:1 Multiple days so log files are split </code></pre> <p><strong>=== EXAMPLE 3 END ===</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload