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 ===
Use the Julian Day
or if you are not in UTC...
where
$tz
is your timezone offset in seconds.Use php5.3's DateInterval class:
outputs:
Round the timestamps down to the nearest 86400 seconds, take the difference, and divide by 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.