排序从特定开始日期开始的天数

时间:2018-10-02 11:27:47

标签: php arrays sorting days

我这几天的日期是随机的:

$jour_planning[] = "friday";
$jour_planning[] = "wednesday";
$jour_planning[] = "monday";
$jour_planning[] = "tuesday";
$jour_planning[] = "thursday";
$jour_planning[] = "sunday";
$jour_planning[] = "saturday";

如果我们今天是“星期二”,我想拥有这个新数组:

$jour_planning[] = "wednesday";
$jour_planning[] = "thursday";
$jour_planning[] = "friday";
$jour_planning[] = "saturday";
$jour_planning[] = "sunday";
$jour_planning[] = "monday";
$jour_planning[] = "tuesday";

使用usort()如何做到?

关于, 范妮

5 个答案:

答案 0 :(得分:1)

我将首先对数组进行排序(并避免使用法语和英语命名:))。

然后,循环将数组排序并存储到2个单独的数组中,该数组位于预期的数组之前(包括该数组)的前几天和之后的几天。

最后,合并两个数组。

肯定有更好的方法。

$jour_planning[] = "friday";
$jour_planning[] = "wednesday";
$jour_planning[] = "monday";
$jour_planning[] = "tuesday";
$jour_planning[] = "thursday";
$jour_planning[] = "sunday";
$jour_planning[] = "saturday";

$sorted_days_planning = array(1 => null, 2 => null, 3 => null, 4 => null, 5 => null, 6 => null, 7 => null);

foreach ($jour_planning as $value)
{
    $day_of_week = date('N', strtotime($value));
    $sorted_days_planning[$day_of_week] = $value;
}

$final_days_planning = array();

$day_to_start = "tuesday";
$day_found = false;
$temp_array = array();
foreach ($sorted_days_planning as $value)
{
    if (!$day_found)
    {
        $temp_array[] = $value;
        if ($day_to_start == $value)
            $day_found = true;
    }
    else
        $final_days_planning[] = $value;
}

$final_days_planning = array_merge($final_days_planning, $temp_array);

var_dump($final_days_planning);

输出:

array(6) {
    [0]=> string(9) "wednesday"
    [1]=> string(8) "thursday"
    [2]=> string(6) "friday"
    [3]=> string(8) "saturday"
    [4]=> string(6) "sunday"
    [5]=> string(6) "monday"
    [6]=> string(7) "tuesday"
}

答案 1 :(得分:0)

由于您的数组是随机排序的,因此没有其他方法可以正确地对其进行排序,然后手动执行(您无法按数字或字母顺序对数组进行排序-可以通过用{{1}计算天数来完成}函数,请参见@Cid's答案),因此:

datetime()

首先,您必须搜索当天,然后将1添加到这一天的索引中,然后您便拥有了明天。现在,有了此索引后,您只需创建2个数组:第一个数组从明天的开始,第二个数组从索引0到今天的开始。然后将这两个数组合并在一起,您便拥有了想要的东西。

答案 2 :(得分:0)

您可以使用uksort()date()strtotime()重新排列数组。

function sort_week_days( $t1, $t2 ) {
        $weekdays = array( 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' );
        foreach ( $weekdays as $key => $value ) {
            $weekdays[ $key ] = date( 'w', strtotime( $value ) );
        }

        $t1_time = date( 'w', strtotime( strtolower( $t1 ) ) );
        $t2_time = date( 'w', strtotime( strtolower( $t2 ) ) );

        return array_search( $t1_time, $weekdays ) - array_search( $t2_time, $weekdays );
    }

您需要按如下所示传递随机数组:

usort($jour_planning, "sort_week_days");

然后您可以在以下代码中使用数组:

$day = date('w'); // You need to change to date('w', '-1day'); get result from today.

for ($i=0; $i <= $day ; $i++) {
   array_push($jour_planning, array_shift($jour_planning));
}   

答案 3 :(得分:0)

您可以使用此功能对工作日进行排序,并且可以选择包括第一天。

function sort_days($day_to_start = 'monday', $include_first_day = true)
{
    $final_days_planning = [];
    $rest_days = [];
    $first_day_found = 0;
    foreach ([ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ] as $day)
    {
        if (!$first_day_found){
            if ($day_to_start == $day){
                $first_day_found = 1;
                if($include_first_day){
                    $final_days_planning[] = $day;
                }
                else
                {
                    $rest_days[] = $day;
                }
            }
            else
            {
                $rest_days[] = $day;
            }
        } else{
            $final_days_planning[] = $day;
        }
    }
    return array_merge($final_days_planning, $rest_days);
}

尝试一下:

$days = sort_days('wednesday');
print_r($days);

答案 4 :(得分:-1)

test1 enable