PHP 每隔一小时打印一次?

发布于 2025-01-01 11:33:30 字数 1963 浏览 4 评论 0原文

有没有比这更简单/更好的方法来获取每秒一个小时,

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';}

我需要这个用于谷歌图表,并想检查这种方法是否愚蠢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

做个ˇ局外人 2025-01-08 11:33:30

1) 获取当前小时

2) mod2(只有两组不同的数字,奇数和偶数)

3) 构建小时数组

4) 按值对数组进行排序

5) 拆分原始小时所在的数组,然后重新组合。

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.

栀梦 2025-01-08 11:33:30
$h = date("H");
$line = '';
for($i=0; $i<=24; $i++)
{
    if($i % 2 == $h % 2)
        $line .= '|' . ($i < 10 ? '0'.$i : $i);
}
$h = date("H");
$line = '';
for($i=0; $i<=24; $i++)
{
    if($i % 2 == $h % 2)
        $line .= '|' . ($i < 10 ? '0'.$i : $i);
}
古镇旧梦 2025-01-08 11:33:30

一种方法是创建一个带有键的数组:

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

然后你可以这样调用它:

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

也可能有一种更好的方法来生成它,但是既然你已经输入了它,它就在那里..如果你想的话那就太糟糕了做出改变。

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.

心在旅行 2025-01-08 11:33:30

不错的代码:)

实际上在 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);

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);
寂寞陪衬 2025-01-08 11:33:30
    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);
    }

使用这个来测试它!

    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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文