Display days of the week in order with PHP

2

I have this html:

<div class="base-semana">
                <div class="dia-semana">
                    <div class="dial">
                        DIA DA SEMANA
                    </div>
                </div>
</div>

Divs with "day-week" class have 7 equals. I would like to display every day of the week in order from the current one.

Eg the first div would be in the case today: Tuesday, and the last Monday. How can I do this?

    
asked by anonymous 09.05.2017 / 22:58

2 answers

2

example - ideone

    $output ="";
    $semana = array(

    '1' => 'Segunda-Feira',
    '2' => 'Terca-Feira',
    '3' => 'Quarta-Feira',
    '4' => 'Quinta-Feira',
    '5' => 'Sexta-Feira',
    '6' => 'Sábado',
    '7' => 'Domingo'
);

$a = array_slice($semana, date('w') - 1 );

$b = array_slice($semana, 0 , date('w') - 1 );

$c = array_merge_recursive( $a , $b );

foreach( $c as $key => $value ) {

    $output = $output . '<div class="dia-semana"><div class="dial">'. $value . "</div></div>" ;
}

$result = '<div class="base-semana">'.$output.'</div>' ; 

echo $result;
  

$ a = returns the sequence of all $ week elements from [date ('w') - 1], where date ('w') is the numeric representation of the day of the week.      

$ b = returns the sequence of the array elements $ week from zero to [date ('w') - 1], with date ('w') being the numeric representation of the day of the week.      

$ c = fuse the elements of $ a with the $ b array so that the elements of $ b are placed at the end of the $ a array.

To set the result according to Brasília time instead of server time, place this line date_default_timezone_set('America/Sao_Paulo'); at the beginning of PHP. Reference - date_default_timezone_set

    
10.05.2017 / 00:15
1
$a = [ 1 => 'seg' , 2 => 'ter' , 3 => 'qua' , 4 => 'qui' , 5 => 'sex' , 6 => 'sab' , 7 => 'dom' ];

$b[] = array_slice($a, date('N') - 1 );
$b[] = array_slice($a, 0 , date('N') - 1 );
$c   = array_merge_recursive( $b[0] , $b[1] );

print_r( $c );

output:

Array
(
    [0] => ter
    [1] => qua
    [2] => qui
    [3] => sex
    [4] => sab
    [5] => dom
    [6] => seg
)

Just apply a loop to the $c variable.

    
09.05.2017 / 23:26