Jump to content

Recommended Posts

I found some php code which relates exactly to what i need doing on my website. I don't think its finished but most of it is apparently done. Could anyone edit it to my needs and explain how I can execute it so that it can change a particular image on the page?  

What I need doing is for an image on the page to be changed according to certain times in the day, the code below only uses hours and not minutes, I also need minutes. 

 

 

<?php

$h = date('G'); //set variable $h to the hour of the day.

$m = date('i'); //set variable $m to the min of the hour.

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

 

 

 

// MONDAY SCHEDULE

if ($d == 1 && $h >= 0 && $h < 4) $img = 'image.jpg';

else if ($d == 1 && $h >= 4 && $h < 8 ) $img = 'image.jpg';

else if ($d == 1 && $h >= 8 && $h < 12) $img = 'image.jpg';

else if ($d == 1 && $h >= 12 && $h < 13) $img = 'image.jpg';

else if ($d == 1 && $h >= 13 && $h < 15) $img = 'image.jpg';

else if ($d == 1 && $h >= 15 && $h < 19) $img = 'image';

else if ($d == 1 && $h >= 19) $img = 'image.jpg';

else if ($d == 2 && $h < 0) $img = 'image.jpg';

 

// TUESDAY SCHEDULE

if ($d == 2 && $h >= 0 && $h < 4) $img = 'image.jpg';

else if ($d == 2 && $h >= 4 && $h < 8 ) $img = 'image.jpg';

else if ($d == 2 && $h >= 8 && $h < 12) $img = 'image.jpg';

else if ($d == 2 && $h >= 12 && $h < 13) $img = 'image.jpg';

else if ($d == 2 && $h >= 13 && $h < 15) $img = 'image.jpg';

else if ($d == 2 && $h >= 15 && $h < 17) $img = 'image.jpg';

else if ($d == 2 && $h >= 17 && $h < 20) $img = 'image.jpg';

else if ($d == 2 && $h >= 20) $img = 'image.jpg';

else if ($d == 3 && $h < 0) $img = 'image.jpg';

 

// WEDNESDAY SCHEDULE

if ($d == 3 && $h >= 0 && $h < 4) $img = 'image.jpg';

else if ($d == 3 && $h >= 4 && $h < 8 ) $img = 'image.jpg';

else if ($d == 3 && $h >= 8 && $h < 12) $img = 'image.jpg';

else if ($d == 3 && $h >= 12 && $h < 13) $img = 'image.jpg';

else if ($d == 3 && $h >= 13 && $h < 15) $img = 'image.jpg';

else if ($d == 3 && $h >= 15 && $h < 19) $img = 'image.jpg';

else if ($d == 3 && $h >= 19) $img = 'image.jpg';

else if ($d == 4 && $h < 0) $img = 'image.jpg';

 

// THURSDAY SCHEDULE

if ($d == 4 && $h >= 0 && $h < 4) $img = 'image.jpg';

else if ($d == 4 && $h >= 4 && $h < 8 ) $img = 'image.jpg';

else if ($d == 4 && $h >= 8 && $h < 12) $img = 'image.jpg';

else if ($d == 4 && $h >= 12 && $h < 13) $img = 'image.jpg';

else if ($d == 4 && $h >= 13 && $h < 15) $img = 'image.jpg';

else if ($d == 4 && $h >= 15 && $h < 19) $img = 'image.jpg';

else if ($d == 4 && $h >= 19) $img = 'image.jpg';

else if ($d == 5 && $h < 0) $img = 'image.jpg';

 

// FRIDAY SCHEDULE

if ($d == 5 && $h >= 0 && $h < 4) $img = 'image.jpg';

else if ($d == 5 && $h >= 4 && $h < 8 ) $img = 'image.jpg';

else if ($d == 5 && $h >= 8 && $h < 10) $img = 'image.jpg';

else if ($d == 5 && $h >= 10 && $h < 12) $img = 'image.jpg';

else if ($d == 5 && $h >= 12 && $h < 13) $img = 'image.jpg';

else if ($d == 5 && $h >= 13 && $h < 15) $img = 'image.jpg';

else if ($d == 5 && $h >= 15 && $h < 18) $img = 'image.jpg';

else if ($d == 5 && $h >= 18 && $h < 20) $img ='image.jpg';

else if ($d == 5 && $h >= 20 && $h < 22) $img = 'image.jpg';

else if ($d == 5 && $h >= 22) $img = 'image.jpg';

else if ($d == 6 && $h < 0) $img = 'image.jpg';

 

// SATURDAY SCHEDULE

else if ($d == 6 && $h >= 4 && $h < 6) $img = 'image.jpg';

else if ($d == 6 && $h >= 6 && $h < 7) $img = 'image.jpg';

else if ($d == 6 && $h >= 7 && $h < 7) $img = 'image.jpg';

else if ($d == 6 && $h >= 7 && $h < 7) $img = 'image.jpg';

else if ($d == 6 && $h >= 7 && $h < 9) $img = 'image.jpg';

else if ($d == 6 && $h >= 9 && $h < 11) $img = 'image.jpg';

else if ($d == 6 && $h >= 11 && $h < 13) $img = 'image.jpg';

else if ($d == 6 && $h >= 13 && $h < 14) $img = 'image.jpg';

else if ($d == 6 && $h >= 14 && $h < 17) $img = 'image.jpg';

else if ($d == 6 && $h >= 17 && $h < 23) { $img = 'image.jpg'; $locutor = 'Byron'; $hora = '5:00 PM - 11:00 PM'; $text = 'ELECTRONIC MUSIC RADIO SHOW'; if(mb_strlen($text)>13) { $text=mb_substr($text, 0, 13); $text.='...'; } } 

else if ($d == 6 && $h >= 23) $img = 'image.jpg';

else if ($d == 0 && $h < 0) $img = 'image.jpg';

 

// SATURDAY SCHEDULE

else if ($d == 0 && $h >= 0 && $h < 2) $img = 'image.jpg';

else if ($d == 0 && $h >= 2 && $h < 4) $img = 'image.jpg';

else if ($d == 0 && $h >= 4 && $h < 5) $img = 'image.jpg';

else if ($d == 0 && $h >= 5 && $h < 6) $img = 'image.jpg';

else if ($d == 0 && $h >= 6 && $h < 8 ) $img = 'image.jpg';

else if ($d == 0 && $h >= 8 && $h < 9) $img = 'image.jpg';

else if ($d == 0 && $h >= 9 && $h < 10) $img = 'image.jpg';

else if ($d == 0 && $h >= 10 && $h < 11) $img = 'image.jpg';

else if ($d == 0 && $h >= 11 && $h < 12) $img = 'image.jpg';

else if ($d == 0 && $h >= 12 && $h < 16) $img = 'image.jpg';

else if ($d == 0 && $h >= 16 && $h < 17) $img = 'image.jpg';

else if ($d == 0 && $h >= 17 && $h < 18) $img = 'image.jpg';

else if ($d == 0 && $h >= 18 && $h < 19) $img = 'image.jpg';

else if ($d == 0 && $h >= 19 && $h < 21) $img = 'image.jpg';

else if ($d == 0 && $h >= 21) $img = 'image.jpg';

else if ($d == 1 && $h < 0) $img = 'image.jpg';

 

echo'<div style="float: left; width: 90px"><img src="'.$img.'" height="80px" width="80px" " /></div><div style="float: left; width: 141px; height: 80px; margin-top: 10px; color: #FFFFFF; font-family: Tahoma; font-size: 15px;">'.$text.' <br \>'.$hora.'<br \> Con: '.$locutor.'</div>';

 

 

 

 

The person who wrote this was using some spanish words at the bottom like "hora" and "locutor". Im not to familiar with php and so if that needs replacing then please replace it with the correct version.

you should be using a database, but lacking that, here's an example showing a general purpose/data driven way of doing this common assignment - 

<?php
// define constants for day names to make reading and writing code easier
define('SUN',0);
define('MON',1);
define('TUE',2);
define('WED',3);
define('THU',4);
define('FRI',5);
define('SAT',6);

// domain root relative path to where images are at
$path = '/images/shows/';

// data - days (one or an array), start-time (inclusive), end-time (exclusive), image
$a[] = array(SUN,'00:00','01:00','MensHealth'); //1
$a[] = array(SUN,'01:00','02:00','AlanTaylor'); //1
$a[] = array(SUN,'02:00','05:00','HughHewitt'); //1 (this image used later w/different hours)
$a[] = array(range(SUN,SAT),'05:00','10:00','RedEyeRadio'); //7
$a[] = array(SUN,'10:00','11:00','MomTalk'); //1
$a[] = array(SUN,'11:00','12:00','GoodParenting'); //1
$a[] = array(SUN,'12:00','14:00','PetShow'); //1
$a[] = array(SUN,'14:00','15:00','GardenRebel'); //1
$a[] = array(SUN,'15:00','16:00','WorkingMother'); //1
$a[] = array(SUN,'16:00','17:00','WhatsCooking'); //1
$a[] = array(SUN,'17:00','18:00','HomeWizards'); //1
$a[] = array(SUN,'18:00','19:00','DougStephan'); //1
$a[] = array(SUN,'19:00','20:00','Finance'); //1
$a[] = array(SUN,'20:00','21:00','PopularScience'); //1
$a[] = array(SUN,'21:00','22:00','ABCRadio'); //1
$a[] = array(SUN,'22:00','24:00','Medicine'); //1

$a[] = array(MON,'00:00','03:00','ArmedAmerica'); //1
$a[] = array(MON,'03:00','05:00','HughHewitt'); //1
$a[] = array(range(MON,FRI),'10:00','13:00','BobRick'); //5
$a[] = array(range(MON,FRI),'13:00','16:00','DougStephan'); //5
$a[] = array(range(MON,FRI),'16:00','19:00','MariluHenner'); //5
$a[] = array(range(MON,FRI),'19:00','20:00','DebbieNigro'); //5
$a[] = array(range(MON,FRI),'20:00','22:00','DaveRamsey'); //5
$a[] = array(range(MON,FRI),'22:00','24:00','JoyBrowne'); //5

$a[] = array(range(TUE,SAT),'00:00','01:00','ShannonJoy'); //5
$a[] = array(range(TUE,SAT),'01:00','02:00','BillNojay'); //5
$a[] = array(range(TUE,SAT),'02:00','05:00','DennisPrager'); //5

$a[] = array(SAT,'10:00','12:00','HaidtReport'); //1
$a[] = array(SAT,'12:00','13:00','ABCNews'); //1
$a[] = array(SAT,'13:00','16:00','GarySullivan'); //1
$a[] = array(SAT,'16:00','18:00','PopularTech'); //1
$a[] = array(SAT,'18:00','19:00','WhatWorks'); //1
$a[] = array(SAT,'19:00','21:00','JillMoney'); //1
$a[] = array(SAT,'21:00','23:00','YouManual'); //1
$a[] = array(SAT,'23:00','24:00','MadeAmerica'); //1

$hm = date('H:i'); //set variable the hour:minute HH:MM of the day.
$d = date('w'); //set variable $d to the day of the week.

// find the image that matches the day/time
$img = '';
foreach($a as $e){
    if(((is_array($e[0]) && in_array($d,$e[0])) || (!is_array($e[0]) && $d == $e[0])) && $hm >= $e[1] && $hm < $e[2] ){
        $img = $e[3];
        break;
    }
}

if($img == ''){
    echo 'no matching program found';
} else {
?>
    <img src="<?php echo $path.$img.'.jpg'; ?>">
<?php
}
Edited by mac_gyver

How many images will you need? Like a dozen or so, or many more? Will you ever want to change them (either the images or the times) and if so how often?

 

Its still early days to know how many images are going to be displayed in a day. Plus, they may vary from 10 a day to even 40. The images would need to change every 15 minutes and as for the images themselves, about 5 or so would stay the same and stay on the website all the time according to certain times in the day.The rest of the images are going to be given to me from different people to place on the website.

  • 2 weeks later...

 

you should be using a database, but lacking that, here's an example showing a general purpose/data driven way of doing this common assignment - 

<?php
// define constants for day names to make reading and writing code easier
define('SUN',0);
define('MON',1);
define('TUE',2);
define('WED',3);
define('THU',4);
define('FRI',5);
define('SAT',6);

// domain root relative path to where images are at
$path = '/images/shows/';

// data - days (one or an array), start-time (inclusive), end-time (exclusive), image
$a[] = array(SUN,'00:00','01:00','MensHealth'); //1
$a[] = array(SUN,'01:00','02:00','AlanTaylor'); //1
$a[] = array(SUN,'02:00','05:00','HughHewitt'); //1 (this image used later w/different hours)
$a[] = array(range(SUN,SAT),'05:00','10:00','RedEyeRadio'); //7
$a[] = array(SUN,'10:00','11:00','MomTalk'); //1
$a[] = array(SUN,'11:00','12:00','GoodParenting'); //1
$a[] = array(SUN,'12:00','14:00','PetShow'); //1
$a[] = array(SUN,'14:00','15:00','GardenRebel'); //1
$a[] = array(SUN,'15:00','16:00','WorkingMother'); //1
$a[] = array(SUN,'16:00','17:00','WhatsCooking'); //1
$a[] = array(SUN,'17:00','18:00','HomeWizards'); //1
$a[] = array(SUN,'18:00','19:00','DougStephan'); //1
$a[] = array(SUN,'19:00','20:00','Finance'); //1
$a[] = array(SUN,'20:00','21:00','PopularScience'); //1
$a[] = array(SUN,'21:00','22:00','ABCRadio'); //1
$a[] = array(SUN,'22:00','24:00','Medicine'); //1

$a[] = array(MON,'00:00','03:00','ArmedAmerica'); //1
$a[] = array(MON,'03:00','05:00','HughHewitt'); //1
$a[] = array(range(MON,FRI),'10:00','13:00','BobRick'); //5
$a[] = array(range(MON,FRI),'13:00','16:00','DougStephan'); //5
$a[] = array(range(MON,FRI),'16:00','19:00','MariluHenner'); //5
$a[] = array(range(MON,FRI),'19:00','20:00','DebbieNigro'); //5
$a[] = array(range(MON,FRI),'20:00','22:00','DaveRamsey'); //5
$a[] = array(range(MON,FRI),'22:00','24:00','JoyBrowne'); //5

$a[] = array(range(TUE,SAT),'00:00','01:00','ShannonJoy'); //5
$a[] = array(range(TUE,SAT),'01:00','02:00','BillNojay'); //5
$a[] = array(range(TUE,SAT),'02:00','05:00','DennisPrager'); //5

$a[] = array(SAT,'10:00','12:00','HaidtReport'); //1
$a[] = array(SAT,'12:00','13:00','ABCNews'); //1
$a[] = array(SAT,'13:00','16:00','GarySullivan'); //1
$a[] = array(SAT,'16:00','18:00','PopularTech'); //1
$a[] = array(SAT,'18:00','19:00','WhatWorks'); //1
$a[] = array(SAT,'19:00','21:00','JillMoney'); //1
$a[] = array(SAT,'21:00','23:00','YouManual'); //1
$a[] = array(SAT,'23:00','24:00','MadeAmerica'); //1

$hm = date('H:i'); //set variable the hour:minute HH:MM of the day.
$d = date('w'); //set variable $d to the day of the week.

// find the image that matches the day/time
$img = '';
foreach($a as $e){
    if(((is_array($e[0]) && in_array($d,$e[0])) || (!is_array($e[0]) && $d == $e[0])) && $hm >= $e[1] && $hm < $e[2] ){
        $img = $e[3];
        break;
    }
}

if($img == ''){
    echo 'no matching program found';
} else {
?>
    <img src="<?php echo $path.$img.'.jpg'; ?>">
<?php
}

 

Hi, thanks for this. Im actually completely new to php. Would you mind telling me where id place this code so that it works with the website? Just how css needs to be linked to the html by the text in the head, how do i link the php if it is to be linked?

for a php produced page, your code should be laid out in this general order - initialization, post method form processing, get method processing, and html page/template. (see the following post for more information about these items - http://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/?hl=%2Binitialization&do=findComment&comment=1513104 )

 

the defined constants, $path, and $a[] statements from the code in this thread are configuration information. they would typically be in a config.php file that you include in the initialization section on the page.

 

the $hm, $d and the foreach(){} loop are part of the get method processing code, they produce data, the $img value, to be used in the html page/template section.

 

the final if(){{}else{} statement that displays a message or outputs the <img ...> tag goes in the html page/template section, at the point where you want to display the image. the html page/template section starts with the <!DOCTYPE tag through to the </html> tag.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.