两个函数需要成为一个我将如何做到这一点

时间:2014-02-11 09:41:40

标签: php date variables

function friday16mei(){
    $ret= "";
    if(time() <= strtotime('17-05-2014')){
     $ret =   'friday 16 mei';
    }
    return $ret;
}
function getoptions(){
    $ret = "";
    $saturday22 = saturday22march();
    if(strlen($saturday22) > 0){
        $ret .= "<option>" . $saturday22 . "</option>";
    }
    $friday16mei = friday16mei();
    return $ret;
}
$Content= '
<div class="content">
    I am signing up for the following date:<br />
    <select name="date[0]">
        '. getoptions() .'
    </select>
</div>
';
echo $Content;

我现在有2个功能,他们需要成为一个如何做到这一点?我需要参数吗?

我可以在将来添加或编辑它。

1 个答案:

答案 0 :(得分:1)

将您的日期检查值和文本存储在一个数组中,并将其与循环进行比较,而不是方法:

function getoptions(){
    $check = array(
        '17-05-2014' => 'friday 16 mei',
        '23-03-2014' => 'saturday 22 march'
    );

    $now = time();
    $result = array();
    foreach($check as $date => $text) {
        if($now  <= strtotime($date)) {
            $result[] = $text;
        }
    }

    $html = '';
    foreach($result as $v) {
        $html .= '<option>'.$v.'</option>';
    }

    return $html;
}
相关问题