Dave
Dave

Reputation: 114

PHP print every second hour?

Is there a easier/better way to get every second hour than this

if(date("H")=='00'){$chart_updates = '|02|04|06|08|10|12|14|16|18|20|22|00';}
if(date("H")=='01'){$chart_updates = '|03|05|07|09|11|13|15|17|19|19|23|01';}
if(date("H")=='02'){$chart_updates = '|04|06|08|10|12|14|16|18|20|21|00|02';}
if(date("H")=='03'){$chart_updates = '|05|07|09|11|13|15|17|19|21|23|01|03';}
if(date("H")=='04'){$chart_updates = '|06|08|10|12|14|16|18|20|22|00|02|04';}
if(date("H")=='05'){$chart_updates = '|07|09|11|13|15|17|19|21|23|01|03|05';}
if(date("H")=='06'){$chart_updates = '|08|10|12|14|16|18|20|22|00|02|04|06';}
if(date("H")=='07'){$chart_updates = '|09|11|13|15|17|19|21|23|01|03|05|07';}
if(date("H")=='08'){$chart_updates = '|10|12|14|16|18|20|22|00|02|04|06|08';}
if(date("H")=='09'){$chart_updates = '|11|13|15|17|19|21|23|01|03|05|07|09';}
if(date("H")=='10'){$chart_updates = '|12|14|16|18|20|22|00|02|04|06|08|10';}
if(date("H")=='11'){$chart_updates = '|13|15|17|19|21|23|01|03|05|07|09|11';}
if(date("H")=='12'){$chart_updates = '|14|16|18|20|22|00|02|04|06|08|10|12';}
if(date("H")=='13'){$chart_updates = '|15|07|19|21|23|01|03|05|07|09|11|13';}
if(date("H")=='14'){$chart_updates = '|16|08|20|22|00|02|04|06|08|10|12|14';}
if(date("H")=='15'){$chart_updates = '|17|09|21|23|01|03|05|07|09|11|13|15';}
if(date("H")=='16'){$chart_updates = '|18|20|22|00|02|04|06|08|10|12|16|16';}
if(date("H")=='17'){$chart_updates = '|19|21|23|01|03|05|07|09|11|13|15|17';}
if(date("H")=='18'){$chart_updates = '|20|22|00|02|04|06|08|10|12|14|16|18';}
if(date("H")=='19'){$chart_updates = '|21|23|01|03|05|07|09|11|13|15|17|19';}
if(date("H")=='20'){$chart_updates = '|22|00|02|04|06|08|10|12|14|16|18|20';}
if(date("H")=='21'){$chart_updates = '|23|01|03|05|07|09|11|13|15|17|19|21';}
if(date("H")=='22'){$chart_updates = '|00|02|04|06|08|10|12|14|16|18|20|22';}
if(date("H")=='23'){$chart_updates = '|01|03|05|07|09|11|13|15|17|19|21|23';}

I need this for google charts and wanted to check if this way is stupid.

Upvotes: 0

Views: 368

Answers (5)

Unex Web'O
Unex Web'O

Reputation: 11

    function helper_add($h,$plus=0){
        if($h+$plus > 23){
            return $h+$plus-24;
        }
        return $h+$plus;
    }
    function helper_10($in){
        return $in < 10 ? '0'.$in : $in;
    }
    function getchartupdates(){
        $now = date('G');
        for($i=($now%2==0?0:1); $i<=24 ;$i+=2)
            $res[] = helper_10(helper_add($now,$i));
        return '|'.implode('|',$res);
    }

used this to test it !

Upvotes: 0

Alienz
Alienz

Reputation: 118

One way is to create an array with keys:

$theHour['00'] = '|02|04|06|08|10|12|14|16|18|20|22|00';

Then you can call it like this:

$chart_updates = $theHour[date("H")];

There is also probably a better way to generate this too, but since you already typed it out, its there.. It would just suck if you want to make a change.

Upvotes: 1

Vyktor
Vyktor

Reputation: 21007

Nice code :)

There's actually much easier way to do this in php:

$chars = array();
$start = date("H")+2;
for( $i = 0; $i < 12; $i++){
    $chars[] = str_pad( ($start+2*$i)%24, 2, '0', STR_PAD_LEFT);
}

$chart_updates = '|' . implode( '|', $chars);

Upvotes: 0

Seagull
Seagull

Reputation: 3620

$h = date("H");
$line = '';
for($i=0; $i<=24; $i++)
{
    if($i % 2 == $h % 2)
        $line .= '|' . ($i < 10 ? '0'.$i : $i);
}

Upvotes: 1

circusdei
circusdei

Reputation: 1967

1) take the current hour

2) mod2 (there are only two different sets of numbers, odd and even)

3) build array of hours

4) sort array by value

5) split array where the original hour was, and recombine.

Upvotes: 4

Related Questions