I have this array of days in a random order :
$jour_planning[] = "friday";
$jour_planning[] = "wednesday";
$jour_planning[] = "monday";
$jour_planning[] = "tuesday";
$jour_planning[] = "thursday";
$jour_planning[] = "sunday";
$jour_planning[] = "saturday";
If we are today a "tuesday", I would like to have this new array :
$jour_planning[] = "wednesday";
$jour_planning[] = "thursday";
$jour_planning[] = "friday";
$jour_planning[] = "saturday";
$jour_planning[] = "sunday";
$jour_planning[] = "monday";
$jour_planning[] = "tuesday";
How can do that, with usort() ?
Regards, Vianney
You can rearrange your array using uksort()
, date()
and 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 );
}
You need to pass your random array as below :
usort($jour_planning, "sort_week_days");
Then you can used your array in below code :
$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));
}