开发者

Efficient way of finding number of steps between array values

Say you have an array representing days of the week that a shop is open based on PHP's date('w'), something like:

$open = array(1,3,4); // open on mon/wed/thu
$we开发者_JS百科ek = array(0,1,2,3,4,5,6); // full week of days according to date('w')

and you have the current day date('w') (e.g. 6 representing Saturday) - whats the most efficient way of calculating the number of days/steps before the next open day?


I'm not sure if efficiency is really an issue here since the small amount of items:

for($i=1;$i<7;$i++) {
if (in_array((($day+$i)%7), $open)) break;
}

echo "Next open day in $i days";


Here's a simple enough way...just look for the next available day:

$daysUntilOpen=0;
while (!in_array($day, $open))
{
   $day=($day+1)%7;
   $daysUntilOpen++;
}

If you're doing lots of lookups, you could build an array to tell you how days until opening time for each day, e.g.

$daysUntilOpenLookup=array();
foreach($week as $day)
{
    $daysUntilOpen=0;
    while (!in_array($day, $open))
    {
       $day=($day+1)%7;
       $daysUntilOpen++;
    }

    $daysUntilOpenLookup[$day]=$daysUntilOpen;
}

Now you can simply do this to find how many days until opening time

$daysUntilOpen=$daysUntilOpenLookup[$day];


For at most seven values on $open you can do a simple linear search:

$nextWeekdayOpen = null;
$daysUntilNextOpen = null;
$currentWeekday = date('w');
foreach ($open as $weekday) {
    if ($weekday >= $currentWeekday) {
        $nextWeekdayOpen = $weekday;
        break;
    }
}
if (is_null($nextWeekdayOpen) && !empty($open)) {
    $nextWeekdayOpen = $open[0];
}
if (!is_null($nextWeekdayOpen)) {
    $daysUntilNextOpen = ($nextWeekdayOpen > $currentWeekday) ? $nextWeekdayOpen-$currentWeekday : 6-$currentWeekday+$nextWeekdayOpen;
}

This simply iterates the open weekdays and looks for the first one that is equal to (i.e. open today) or greater than the current weekday. If there is none, it takes the first open weekday (assuming open weekdays are sorted). Finally the days between the next open weekday and the current weekday is calculated.

This algorithm runs in linear time complexity.


This is the way I would do it, using the very helpful strtotime() function. It's also very clear how it works.

<?php
// Get the current timestamp for comparison
$now = time();

// Get unix timestamps for each open day relative to comparison time $now
$monday = strtotime("next monday", $now);
$tuesday = strtotime("next wednesday", $now);
$thursday = strtotime("next thursday", $now);

// Put values in to array
$days_open = array($monday, $tuesday, $thursday);

// See which unix timestamp is least (the nearest to current day)
$nearest_day = min($days_open);

// Then you can get difference in days
$days_difference =  date('w', $nearest_day) - date('w', $now);

// You can show the difference like this
echo "We're next open in " , $days_difference , " days";
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜