lice200 Posted December 21, 2006 Share Posted December 21, 2006 Hello, I am trying to write code for a script that will take a set time, compare it to the current time, then echo a set link for that time slot. The thing is i have not been able to do this thus far with php. One of my problems is with if statements. When I use if statements the script becomes unstable and gives off results.At the moment I have some code written for the script. (See Code One) The problem is that when $curtime clicks over to 2000 (24/h format) the script stops working. Another problem is if $timestr is higher then $timstr2 the script again, stops working. So I am wondering, is there a better way to make this work. If you can give me a place to start that would be the first step in helping me make this work. Maybe you can write something out for me to help me compare and get me out of this block.Code One: [code=php:0]<?php //Specify the start time and the delay$timestr = "1200";$link1 = "Link One";$timestr2 = "1400";$link2 = "Link Two";$timestr3 = "1500";$link3 = "Link Three";$curtime = date('Hi'); //Get the current time in format 24/h echo "Current Time: $curtime<br>"; if ($curtime >= $timestr) { // Script start. $link = $link1; } if ($curtime >= $timestr2) { $link = $link2; } if ($curtime >= $timestr3) { $link = $link3;}echo $link;?>[/code] Link to comment https://forums.phpfreaks.com/topic/31472-solved-start-time-script/ Share on other sites More sharing options...
Barand Posted December 21, 2006 Share Posted December 21, 2006 What link do you want displayed prior to 1200?Running this[code]<?php //Specify the start time and the delay$timestr = "1200";$link1 = "Link One";$timestr2 = "1400";$link2 = "Link Two";$timestr3 = "1500";$link3 = "Link Three";# $curtime = date('Hi'); //Get the current time in format 24/hfor ($curtime = '0000'; $curtime < '2400'; $curtime += 100) { echo "<br>Current Time: $curtime : "; if ($curtime >= $timestr) { // Script start. $link = $link1; } if ($curtime >= $timestr2) { $link = $link2; } if ($curtime >= $timestr3) { $link = $link3; } echo $link;}?>[/code]gives[pre]Current Time: 0000 : Current Time: 100 : Current Time: 200 : Current Time: 300 : Current Time: 400 : Current Time: 500 : Current Time: 600 : Current Time: 700 : Current Time: 800 : Current Time: 900 : Current Time: 1000 : Current Time: 1100 : Current Time: 1200 : Link OneCurrent Time: 1300 : Link OneCurrent Time: 1400 : Link TwoCurrent Time: 1500 : Link ThreeCurrent Time: 1600 : Link ThreeCurrent Time: 1700 : Link ThreeCurrent Time: 1800 : Link ThreeCurrent Time: 1900 : Link ThreeCurrent Time: 2000 : Link ThreeCurrent Time: 2100 : Link ThreeCurrent Time: 2200 : Link ThreeCurrent Time: 2300 : Link Three[/pre] Link to comment https://forums.phpfreaks.com/topic/31472-solved-start-time-script/#findComment-145819 Share on other sites More sharing options...
lice200 Posted December 21, 2006 Author Share Posted December 21, 2006 [quote author=Barand link=topic=119506.msg489576#msg489576 date=1166708041]What link do you want displayed prior to 1200?[/quote]It should be Link Three because it is the last one. So;Current Time: 1500/0000 -through 1100 : Link Three Link to comment https://forums.phpfreaks.com/topic/31472-solved-start-time-script/#findComment-145886 Share on other sites More sharing options...
Barand Posted December 21, 2006 Share Posted December 21, 2006 try[code]<?php echo "<br>Current Time: $curtime : "; $link = $link3; if ($curtime >= $timestr) { // Script start. $link = $link1; } if ($curtime >= $timestr2) { $link = $link2; } if ($curtime >= $timestr3) { $link = $link3; } echo $link;>[/code] Link to comment https://forums.phpfreaks.com/topic/31472-solved-start-time-script/#findComment-146079 Share on other sites More sharing options...
lice200 Posted December 22, 2006 Author Share Posted December 22, 2006 Barand, If you had to write the same script, how would you do it? Can you help me out more help, this doesn't seem the best way to write it. Thanks. Link to comment https://forums.phpfreaks.com/topic/31472-solved-start-time-script/#findComment-146301 Share on other sites More sharing options...
Barand Posted December 22, 2006 Share Posted December 22, 2006 [code]<?php $times = array ( '0000' => 'Link Three', '1200' => 'Link One', '1400' => 'Link Two', '1500' => 'Link Three',);# $curtime = date('Hi'); //Get the current time in format 24/hfor ($curtime = '0000'; $curtime < '2400'; $curtime += 100) { foreach ($times as $t => $l) { if ($curtime >= $t) $link = $l; } printf ("Current Time: %04d : %s<br>",$curtime,$link); }?>[/code] Link to comment https://forums.phpfreaks.com/topic/31472-solved-start-time-script/#findComment-146354 Share on other sites More sharing options...
lice200 Posted December 22, 2006 Author Share Posted December 22, 2006 Do you see any problems with the changed code below? Such as errors, things that might cause a problem down the road?(I didnt change the fprint line yet, but i will)[code=php:0]<?php $times = array ( '0000' => 'Link Three', '0600' => 'Link One', '1400' => 'Link Two', '1500' => 'Link Three',); $curtime = date('Hi'); //Get the current time in format 24/h#for ($curtime = '0000'; $curtime < '2400'; $curtime += 100) { foreach ($times as $t => $l) #{ if ($curtime >= $t) $link = $l;{ printf ("Current Time: %04d : %s<br>",$curtime,$link); }?>[/code] Link to comment https://forums.phpfreaks.com/topic/31472-solved-start-time-script/#findComment-146382 Share on other sites More sharing options...
Barand Posted December 22, 2006 Share Posted December 22, 2006 remove the test loop code and uncomment the line which gets the current time[code]<?php $times = array ( '0000' => 'Link Three', '1200' => 'Link One', '1400' => 'Link Two', '1500' => 'Link Three',);$curtime = date('Hi'); //Get the current time in format 24/hforeach ($times as $t => $l) { if ($curtime >= $t) $link = $l;}printf ("Current Time: %04d : %s<br>",$curtime,$link); ?>[/code] Link to comment https://forums.phpfreaks.com/topic/31472-solved-start-time-script/#findComment-146383 Share on other sites More sharing options...
lice200 Posted December 22, 2006 Author Share Posted December 22, 2006 Lawl thanks for posting after me :( you have me confused enough! heeh Link to comment https://forums.phpfreaks.com/topic/31472-solved-start-time-script/#findComment-146385 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.