Pregunta

No this is not the standard +86400 seconds between dates.

$start_time = strtotime("2012-01-15 23:59");
$end_time = strtotime("2012-01-16 00:05");

$daysInBetweenTimestamps = ?

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.

Updating with the test for the first example:

echo "Absolute Start: ".date("Y-m-d H:i:s",$start)."<br />";
echo "Absolute End: ".date("Y-m-d H:i:s",$end)."<br />";
echo "Interval Used: $interval(seconds) OR ".($interval / 60)."(minutes)<br />";
$numberOfIntervals = ceil(($end - $start) / $interval);
echo "Number of intervals:$numberOfIntervals<br /><br />";
if ($numberOfIntervals > 0){
    for ($i = 0; $i < $numberOfIntervals; $i++){
        $curStart = $start + ($interval * $i);
        $curEnd = $curStart + $interval;
        if ($curEnd > $end){$curEnd = $end;}
        echo "Interval Start DateTime: ".date("Y-m-d H:i:s",$curStart)."<br />";
        echo "Interval End DateTime: ".date("Y-m-d H:i:s",$curEnd)."<br />";
/* EXAMPLE PHP5.3 DateTime START - NOT WORKING */
        $startDiff = new DateTime("@$curStart");
        $endDiff = new DateTime("@$curEnd");
        $diff = $startDiff->diff($endDiff);
        echo $diff->format("%a") . " days<br />";
        if ($diff->format("%a") > 0){
/* EXAMPLE PHP5.3 DateTime END */
/* EXAMPLE Julian START - WORKS */
            $days = unixtojd($curEnd) - unixtojd($curStart);
            echo "Number of days:$days<br />";
            if ($days > 0){
/* EXAMPLE Julian END */
                // Multiple days so the log files are split
                echo "Multiple days so log files are split<br />";
            }else{
                echo "Single day so log files are NOT split<br />";
            }
        }
    }

Output looks as follows:

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

=== EXAMPLE 1 START ===

0 days
Single day so log files are NOT split

Am I just missing something on the diff?

=== EXAMPLE 1 END ===

=== EXAMPLE 3 START ===

Number of days:1
Multiple days so log files are split

=== EXAMPLE 3 END ===

¿Fue útil?

Solución

Use the Julian Day

$days = unixtojd($t1) - unixtojd($t2);

or if you are not in UTC...

$days = unixtojd($t1 - $tz) - unixtojd($t2 - $tz);

where $tz is your timezone offset in seconds.

Otros consejos

Use php5.3's DateInterval class:

$now = new DateTime();
$then = new DateTime("@123456789"); //this is your timestamp

$diff = $now->diff($then);

echo "then: " . $then->format("Y-m-d") . "\n";
echo $diff->format("%a") . " days\n";

outputs:

then: 1973-11-29
13937 days

Round the timestamps down to the nearest 86400 seconds, take the difference, and divide by 86400:

$start_time = strtotime("2012-01-15 23:59");
$end_time = strtotime("2012-01-16 00:05");

$start_time -= $start_time % 86400;
$end_time -= $end_time % 86400;

$days = ($end_time - $start_time) / 86400;

The rounding down makes each timestamp midnight of that day, and the division gives you the number of days since the epoch. This will only work in UTC, though.

<?php
$start_time = strtotime("2012-01-15 23:59");
$end_time = strtotime("2012-01-16 00:05");

$time_zone = 19800; # My time zone: UTC+0530 hours = UTC+19800 seconds

$days = intval(($end_time + $time_zone) / 86400) -
        intval(($start_time + $time_zone) / 86400);

echo "$days\n";
?>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top