Jump to content

[SOLVED] Start Time Script


lice200

Recommended Posts

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

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/h

for ($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 One
Current Time: 1300 : Link One
Current Time: 1400 : Link Two
Current Time: 1500 : Link Three
Current Time: 1600 : Link Three
Current Time: 1700 : Link Three
Current Time: 1800 : Link Three
Current Time: 1900 : Link Three
Current Time: 2000 : Link Three
Current Time: 2100 : Link Three
Current Time: 2200 : Link Three
Current Time: 2300 : Link Three
[/pre]
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]
[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/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]
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]
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/h

foreach ($times as $t => $l) {
        if ($curtime >= $t) $link = $l;
}
printf ("Current Time: %04d : %s<br>",$curtime,$link);   
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.