How to change the background of an image in CSS code with PHP

Asked

Viewed 1,175 times

2

I would like to know how to make the following change the background image background img in css by day of the week or is Monday to Sunday so that for each day of the week appear a different image in the background that in css code how can I do this using php.

Note: In case this fund is applied in a div as a class because these funds will be used in a skin. From now on I thank anyone who can help me.

  • If it’s just a class you want to change, you can do it in the HTML that PHP creates. Can you show the line of HTML/PHP in this code? what classes you want to give on what day?

  • The class would be . skin_media and the days of the week would be Domindo, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. And the code I didn’t even put together.

  • But what element do you want to apply? <img>? <div>?

  • In a div as class in case the skin will be used several times.

1 answer

2


To know the number of the day of the week you can use the date() thus:

$ds= date("w");

Sunday is a number 0, second 1 and so on. This way you can have an array with the images you want to use:

$imagens = array('domingo.jpg', 'segunda.jpg', etc...);

and then in your code:

<div style="background-image: url(<?php echo $imagens[$ds]; ?>)"></div>

You could also have a class for each day already prepared in CSS, and in the PHP array you have the names of those classes. In that case you would use:

<div class="<?php echo $imagens[$ds]; ?>)"></div>
  • Thanks for the help in case the items of the array will be put following the placement order in the array in the case how are seven images will pick up the first as Sunday and the second forward with other days of the week right ? If you put more than 7 in the case will change the same style following the order of the array until the cycle ends ?

  • @Exact Rodrigo, Sunday is 0, second 1, etc....

  • If you pass 7 items in case I put 14 images will follow the cycle of the array until it finishes so that the 8 image takes as Sunday and the 9 image ahead the other days of the week ?

  • @Rodrigo when you use $imagens[$ds]; the $ds has a number of 0 to 7 and will be used as an array input. It will choose images from 1 to 7 and will return to the first one even if you have more images in the array. If you have more pictures than 7 you can create an algorithm to draw or else know the number of the week of the year and see if it is even or odd. This way you can read 14 images cyclically.

Browser other questions tagged

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