Hi I have this PHP code which I expect should be working however it is currently showing a blank page.
How can I get this working on my HTML site? As when I look on the php file on the local host it doesnt come up with anything just blank. What I want this for is so that different times of the day a different images appear, this is for a radio show. But I cant seem to get it working properly.
<?php
$h = date('G'); //set variable $h to the hour of the day
$d = date('w'); //set variable $d to the day of the week.
$year = date('Y'); //set variable $year to the current year
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
// Adjust 2 hour offset for MST below.
$h = $h-2;
// MONDAY SCHEDULE
if ($d == 1 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h < 0) $img = 'img/hosts/test2.jpg';
// TUESDAY SCHEDULE
if ($d == 2 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h < 0) $img = 'img/hosts/test2.jpg';
// WEDNESDAY SCHEDULE
if ($d == 3 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h < 0) $img = 'img/hosts/test2.jpg';
// THURSDAY SCHEDULE
if ($d == 4 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h < 0) $img = 'img/hosts/test2.jpg';
// FRIDAY SCHEDULE
if ($d == 5 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h < 0) $img = 'img/hosts/test2.jpg';
you do not have a default image
you do not echo the image to the page at the end of the page
your schedules are the same for each day and your are setting the same image for every time
I would expect this:
http://sandbox.onlinephpfunctions.com/code/556ecb6463298fcfa82f93372a000b9738d911d9
<?php
$h = date('G'); //set variable $h to the hour of the day
$d = date('w'); //set variable $d to the day of the week.
$year = date('Y'); //set variable $year to the current year
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
// Adjust 2 hour offset for MST below.
$h = $h-2;
$img="img/hosts/off_air.jpg"; // default
if ($h >= 12 && $h < 14) $img = "img/hosts/test$d_12to14.jpg";
else if ($h >= 14 && $h < 16) $img = "img/hosts/test$d_144to16.jpg";
else if ($h >=16 && $h < 18) $img = "img/hosts/test$d_16to18.jpg";
else if ($h >= 18 && $h < 20) $img = "img/hosts/test$d_18to20.jpg";
else if ($h >= 20 && $h < 22) $img = "img/hosts/test$d_20to22.jpg";
else if ($h >= 22 && $h < 24) $img = "img/hosts/test$d_22to24.jpg";
//else if ($h >= 19) $img = 'img/hosts/test2.jpg'; // already handled
else if ($h < 12) $img = 'img/hosts/test$d_morning.jpg';
echo "<img src=\"$img\" />";
?>
Now you can have images like
test1_12to14.jpg for Monday from 12 to 14
and
test3_22to24.jpg for Wednesday from 22 to 24
you have not declare the scope or condition for hour less than 12
your first condition should be
if ($d == day_number && $h < 12) $img = 'img/hosts/test2.jpg';
this is your code
I have four greeting images which I intend to show based on what time user enters the site.
$morning = "img/morning.png";
$afternoon = "img/afternoon.png";
$evening = "img/evening.png";
$night = "img/night.png";
And I have some conditional statements to assign the values to $cover variable. When I tested, the conditional statement doesn't work.
date_default_timezone_set('Asia/Yangon');
$Hour = date('G');
if($Hour >= 5 && $Hour <= 11) {
$cover = $morning;
}elseif ($Hour >= 11 && $Hour <= 4) {
$cover = $afternoon;
}elseif ($Hour >= 4 && $Hour <= 9){
$cover = $evening;
}elseif ($Hour >= 9 && $Hour <= 4) {
$cover = $night;
}
Img tag
<img class="card-img-top" src="<?php echo $cover; ?>" alt="Not Available" >
if($Hour >= 0 && $Hour <= 11) {
$cover = $morning;
}
elseif ($Hour > 11 && $Hour <= 16) {
$cover = $afternoon;
}
elseif ($Hour > 16 && $Hour <= 19){
$cover = $evening;
}
else{
$cover = $night;
}
Above code will check your hours from 00:00 until 24:00 next day. I fixed your if-else statements so they make more sense in a way that there is a flow in the times.
G 24-hour format of an hour without leading zeros 0 through 23
$hour = date('H', time());
if( $hour > 6 && $hour <= 11) {
$cover = $morning;
}
else if($hour > 11 && $hour <= 16) {
$cover = $afternoon;
}
else if($hour > 16 && $hour <= 23) {
$cover = $evening;
}
else {
$cover = $night;
}
Construct an instance of date and acquire the hour from it
I'm trying to identify the current work shift from 3 options (24 hour format)
First shift 06:00 to 13:59
Second shift 14:00 to 21:59
Third shift 22:00 to 05:59
I tried this, but it's not working as expected
$hour = date("0500");
$shift;
if ($hour >= 0600 && $hour <= 1359 ) {
$shift = 1;
}else if($hour >= 14 && $hour <= 2159 )
{
$shift = 2;
}else
{
$shift = 3;
}
Maybe:
$hour = data('H');
if($hour >= 6 && $hour < 14) {
$shift = 1;
} else if($hour >= 14 && $hour < 22) {
$shift = 2;
} else {
$shift = 3;
}
You could try something like this perhaps:
$hour = data('H');
switch( true ){
case ( $hour >= 6 && $hours < 14 ):$shift=1; break;
case ( $hour >=14 && $hour < 22 ):$shift=2; break;
default: $shift=3; break;
}
Put your time in quotes otherwise starting with 0 makes it an octal number
Also stick with one format if you are going to be comparing
$hour = date("Hi", strtotime("05:00"));
$shift;
if ($hour >= "0600" && $hour <= "1359" ) {
$shift = 1;
}else if($hour >= "1400" && $hour <= "2159" ) {
$shift = 2;
}else {
$shift = 3;
}
I am trying to count number of working days available for particular hours set. here i just need to exclude Sundays by the following php script. if this script find a Sunday this should increase the count. its working but,
This script is capable to exclude first 'Sunday' but not the 'second' and 'third'.
kindly give me a solution to correct this
function testRange() {
$phone_Quantity = 0;
$phone_Quantity = $_POST['phoneQuantity'];
if ($phone_Quantity > 0 && $phone_Quantity <= 300) {
return 300;
} elseif ($phone_Quantity >= 301 && $phone_Quantity <= 600) {
return 600;
} elseif ($phone_Quantity >= 601 && $phone_Quantity <= 900) {
return 900;
} elseif ($phone_Quantity >= 601 && $phone_Quantity <= 1200) {
return 1200;
} elseif ($phone_Quantity >= 1201 && $phone_Quantity <= 1500) {
return 1500;
} elseif ($phone_Quantity >= 1501 && $phone_Quantity <= 1800) {
return 1800;
}
}
echo testRange();
$query_to_get_hours = "SELECT
cdma_filtering_target_hours.Target_hours
FROM
cdma_filtering_target_hours
WHERE
cdma_filtering_target_hours.No_of_units='" . testRange() . "'
";
$query_to_get_hours_query = $system->prepareSelectQuery($query_to_get_hours);
foreach ($query_to_get_hours_query as $THours) {
$targeted_hours = $THours['Target_hours'];
}
$hid = 24; // Hours in a day - could be 24, 48, etc
$days = round($targeted_hours / $hid);
for ($xdays = 0; $xdays < $days; $xdays++) {
if (date('l', strtotime(date('y-m-d', strtotime("+$xdays days")))) == 'Sunday') {
$days++;
break;
}
}
Why do you converting +$xdays string representation twice?
If you comment your if statement and add next line
echo date('l', strtotime("+$xdays days"));
you can clearly see that it works.
Short version: I'm looking to create multiple conditions in an if/then statement which all need to be met. One of those conditions is an array.
Problem: It's ignoring my final condition/array.
Long story: I run a game with a strange calendar system, seen : http://www.beqanna.com/forum/bqcalendar.php. I'm trying to have something on the left side where it says "current season" spit out the current season. I'm having a lot of problems with it understanding the "month".
$d = date('d');
$m = date('m');
$season = '';
If ($d > '1' && $d < '15' && ($m == '01' or '03' or '05' or '07' or '09' or '11')) {
$season = 'Winter';
} If ($d > '16' && $d < '18' && ($m == '01' or '03' or '05' or '07' or '09' or '11')) {
$season = 'Spring';
} If ($d > '19' && $d < '23' && ($m == '01' or '03' or '05' or '07' or '09' or '11')) {
$season = 'Spring, Birthing';
} If ($d > '24' && $d < '31' && ($m == '01' or '03' or '05' or '07' or '09' or '11')) {
$season = 'Spring';
} If ($d > '1' && $d < '15' && ($m == '02' or '04' or '06' or '08' or '10' or '12')) {
$season = 'Summer';
} If ($d > '16' && $d < '18' && ($m == '02' or '04' or '06' or '08' or '10' or '12')) {
$season = 'Autumn';
} If ($d > '19' && $d < '23' && ($m == '02' or '04' or '06' or '08' or '10' or '12')) {
$season = 'Autumn, Breeding';
} If ($d > '24' && $d < '31' && ($m == '02' or '04' or '06' or '08' or '10' or '12')) {
$season = 'Autumn';
}
Other things I've tried -
$m == array (01,03,05,07,09)
$m == (%2) - for the even number months
I've tried moving my ' around the entire thing, adding and subtracting ( ). My final conclusion is that - for whatever reason - it isn't "listening" to the month. Like right now it should say "winter" but it keeps reading "Summer," which is the 1-15 date (correct) but wrong month.
Any suggestions?
First of all your first expression will always return true.
&& ($m == '01' or '03' or '05' or '07' or '09' or '11')
// $m == 1 OR tru or true... and becomes false or true or true
You could use in_array.
Here is the code you need.
<?php
$d = (int)date('d');
$m = (int)date('m');
$season = '';
//echo $d . ' ' . $m . PHP_EOL;
$winterMonths = [1,3,5,7,9,11];
$summerMonths = [2,4,6,8,10,12];
If ($d > 1 && $d < 15 && in_array($m,$winterMonths)) {
$season = 'Winter';
} else if ($d > 16 && $d < 18 && in_array($m,$winterMonths)) {
$season = 'Spring';
} else if ($d > 19 && $d < 23 && in_array($m,$winterMonths)) {
$season = 'Spring, Birthing';
} else if ($d > 24 && $d < 31 && in_array($m,$winterMonths)) {
$season = 'Spring';
} else if ($d > 1 && $d < 15 && in_array($m,$summerMonths)) {
$season = 'Summer';
} else if ($d > 16 && $d < 18 && in_array($m,$summerMonths)) {
$season = 'Autumn';
} else if ($d > 19 && $d < 23 && in_array($m,$summerMonths)) {
$season = 'Autumn, Breeding';
} else if ($d > 24 && $d < 31 && in_array($m,$summerMonths)) {
$season = 'Autumn';
}
echo $season;
Hope it helps.
You've forgot the else statement.
And you currently check if the date is above 1 and under 15. So your routine won't work for dates like 15, 16, 18, 19, 23, 24, 31.
You have to change the operator > to >=, so you will check if the date is above OR equal to the if.
For example your old code:
if ($d > 1 && ....)
If your date is 1, it won't go into this if.
New code:
if ($d >= 1 && ....)
If your date is 1, it will go into this if.
Here the complete routine:
$d = date('d');
$m = date('m');
$season = '';
$winterMonths = array(1,3,5,7,9,11);
$summerMonths = array(2,4,6,8,10,12);
if ($d >= 1 && $d <= 15 && in_array($m, $winterMonths)) {
$season = 'Winter';
} else if ($d >= 16 && $d <= 18 && in_array($m, $winterMonths)) {
$season = 'Spring';
} else if ($d >= 19 && $d <= 23 && in_array($m, $winterMonths)) {
$season = 'Spring, Birthing';
} else if ($d >= 24 && $d <= 31 && in_array($m, $winterMonths)) {
$season = 'Spring';
} else if ($d >= 1 && $d <= 15 && in_array($m, $summerMonths)) {
$season = 'Summer';
} else if ($d >= 16 && $d <= 18 && in_array($m, $summerMonths)) {
$season = 'Autumn';
} else if ($d >= 19 && $d <= 23 && in_array($m, $summerMonths)) {
$season = 'Autumn, Breeding';
} else if ($d >= 24 && $d <= 31 && in_array($m, $summerMonths)) {
$season = 'Autumn';
}
Hope this helps.