English 中文(简体)
PHP 运行,接收 cr,返回下台运行时间
原标题:PHP function that receive a cron string and return the next run timestamp

我需要开发一个任务系统,以便能够在不支持信标的服务器上工作。

我 asking问,是否有任何现行法典可以采取严厉措施(如0,12 1*/2* ,并将下个排定的行程的时间回去)。

如果找不到这样的法典,那么我应如何开始这样做?

最佳回答
问题回答

利用这一职能:

function parse_crontab($time, $crontab)
         {$time=explode(   , date( i G j n w , strtotime($time)));
          $crontab=explode(   , $crontab);
          foreach ($crontab as $k=>&$v)
                  {$time[$k]=intval($time[$k]);
                   $v=explode( , , $v);
                   foreach ($v as &$v1)
                           {$v1=preg_replace(array( /^*$/ ,  /^d+$/ ,  /^(d+)-(d+)$/ ,  /^*/(d+)$/ ),
                                             array( true , $time[$k]. === ,  (1<= .$time[$k].  and  .$time[$k]. <=2) , $time[$k]. %1===0 ),
                                             $v1
                                            );
                           }
                   $v= ( .implode(  or  , $v). ) ;
                  }
          $crontab=implode(  and  , $crontab);
          return eval( return  .$crontab. ; );
         }
var_export(parse_crontab( 2011-05-04 02:08:03 ,  */2,3-5,9 2 3-5 */2 * ));
var_export(parse_crontab( 2011-05-04 02:08:03 ,  */8 */2 */4 */5 * ));

我发现,分裂主义有了一个巨大的答案,但找到了一个关键的挑战。

页: 1 该守则给出了一种条件性<代码>08=0,而这种回归是真实的,因为PHP对从零起算的编号进行了解释,而08和09则不是有效的居住号码,因此它们重新解释为0。 这方面的更多信息。

如何防止PHP在条件条件下进行职业数学? (平均=0)

这里有一套固定的、经过充分评论的分裂主义法典。

// Parse CRON frequency
function parse_crontab($time, $crontab) {
    // Get current minute, hour, day, month, weekday
    $time = explode(   , date( i G j n w , strtotime($time)));
    // Split crontab by space
    $crontab = explode(   , $crontab);
    // Foreach part of crontab
    foreach ($crontab as $k => &$v) {
        // Remove leading zeros to prevent octal comparison, but not if number is already 1 digit
        $time[$k] = preg_replace( /^0+(?=d)/ ,   , $time[$k]);
        // 5,10,15 each treated as seperate parts
        $v = explode( , , $v);
        // Foreach part we now have
        foreach ($v as &$v1) {
            // Do preg_replace with regular expression to create evaluations from crontab
            $v1 = preg_replace(
                // Regex
                array(
                    // *
                     /^*$/ ,
                    // 5
                     /^d+$/ ,
                    // 5-10
                     /^(d+)-(d+)$/ ,
                    // */5
                     /^*/(d+)$/ 
                ),
                // Evaluations
                // trim leading 0 to prevent octal comparison
                array(
                    // * is always true
                     true ,
                    // Check if it is currently that time, 
                    $time[$k] .  === ,
                    // Find if more than or equal lowest and lower or equal than highest
                     (1<=  . $time[$k] .   and   . $time[$k] .  <=2) ,
                    // Use modulus to find if true
                    $time[$k] .  %1===0 
                ),
                // Subject we are working with
                $v1
            );
        }
        // Join 5,10,15 with `or` conditional
        $v =  (  . implode(  or  , $v) .  ) ;
    }
    // Require each part is true with `and` conditional
    $crontab = implode(  and  , $crontab);
    // Evaluate total condition to find if true
    return eval( return   . $crontab .  ; );
}

In the parse_crontab function:

Replace $time[$k] with intval($time[$k]) inside the preg_replace line
to compare two base10 numbers correctly.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签