View weekdays in order with PHP

Asked

Viewed 873 times

1

I have this html:

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

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

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

  • Just this HTML? Have some PHP?

  • It only has html, I want to do php to display what I want, you know? However there are 7 Ivs of the class "day-week" within the "base-week" put only one to decrease the post.

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], date('w') being the numerical representation of the day of the week.

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

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

To set the result according to the time of Brasilia instead of the time of the server put this line date_default_timezone_set('America/Sao_Paulo'); at the beginning of PHP. Reference - date_default_timezone_set

  • 4

    Since your solution has been accepted, it has how to describe what happens when calling the functions array_* for the result to be as expected?

  • 1

    First the solution, then the newsroom.

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 variable $c.

  • It didn’t work, I guess you didn’t understand very well. It’s for each div to display a different day in order.

  • Look at this print and see what it’s like: http://prnt.sc/f5z7di

  • [...]primeira div seria no caso hoje: Terça-Feira, e na última Segunda-Feira[...] The output is exactly what you said... start on Tuesday and Monday at the end... Explain better then your problem.

  • I showed you how to reorder the days of the week based on the current day, just apply a loop to the $c variable to build your html.

  • I don’t want to do an array, but rather display the data in order from the current day, Tuesday was an example counting from today. Check the print.

  • 1

    But from the array you are able to display what you want. If you are not familiar with repeat loops, read here.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.