I'm using PHP 5.3.29 on a WordPress 4.7.4 Site.
I want to change backgrounds based on the time of day. I found this this code here
I have php file header.php
In this file to use html code I am doing:
header.php:
<?php
$time = date("H");
if( $time >= 06 && $time < 10 )
$img_name = 'sunrise.jpg';
if( $time >= 10 && $time < 17 )
$img_name = 'day.jpg';
if( $time >= 17 && $time < 19 )
$img_name = 'sunset.jpg';
if( $time >= 19 && $time < 6 )
$img_name = 'night.jpg';
?>
<style>
body.custom-background{background-image:url('http://website.com/wp-content/uploads/2017/04/<?php echo $img_name;?>');}
</style>
I'm not seeing the $img_name variable passed into this style tag http://website.com/wp-content/uploads/2017/04/
I think its similar to this post php variable inside echo 'html code'
I took a stab at using Heredocs with no luck:
testing using Heredocs
<?php
$time = date("H");
if( $time >= 06 && $time < 10 )
$img_name = 'sunrise.jpg';
if( $time >= 10 && $time < 17 )
$img_name = 'day.jpg';
if( $time >= 17 && $time < 19 )
$img_name = 'sunset.jpg';
if( $time >= 19 && $time < 6 )
$img_name = 'night.jpg';
?>
echo <<<_EOI_
<style>
body.custom-background{background-image:url('http://website.com/wp-content/uploads/2017/04/"$img_name"');}
</style>
_EOI_;
url()${$img_name}instead of"$img_name"and you are also closing the php tag beforeecho