随机问候语功能 - PHP

时间:2014-11-09 20:51:58

标签: php function date time

所以,我在PHP中创建了一个脚本,它根据日期和时间设置问候语。代码检查任何季节性事件,如果没有,则检查时间并分配适当的问候语。我遇到的问题是:

我已设置值以检查时间是否超过18:00,如果超过此时间,则应设置晚上问候语。我的问题是,如果我将php if参数值更改为将来的某个时间,它仍会将问候语保持为晚间问候语。可能有什么不对?

这是我的代码:

$morningGreetings = array(
    1 => "Good morning",
    2 => "Morning",
);

$standardGreetings = array(
    1 => "Hello",
    2 => "Howdy",
    3 => "Hiya",
    4 => "Greetings",
    5 => "Great to see you",
    6 => "Hi there",
    7 => "Hi",
    8 => "Good day",
);

$eveningGreetings = array(
    1 => "Evening",
    2 => "Good evening",
);

$seasonalGreetings = array( 
    1 => "Merry Christmas",
    2 => "Happy New Year",
);

$eventGreetings = array(
    1 => "Happy Birthday",
);

if (date("d m") != strtotime("25 12")) {

    if (date("d m") != strtotime("0 0")) {

        if (date("G i") <= strtotime("18 00")) {

            if (date("G i") <= strtotime("09 00")) {

                $sizeOfArray = sizeof($standardGreetings);

                $greetingValue = rand(1, $sizeOfArray);

                $greeting = $standardGreetings[$greetingValue];

            } else {

                $sizeOfArray = sizeof($morningGreetings);

                $greetingValue = rand(1, $sizeOfArray);

                $greeting = $morningGreetings[$greetingValue];

            }

        } else {

            $sizeOfArray = sizeof($eveningGreetings);

            $greetingValue = rand(1, $sizeOfArray);

            $greeting = $eveningGreetings[$greetingValue];

        }

    } else {

        $greeting = $seasonalGreetings[2];

    }

} else {

    $greeting = $seasonalGreetings[1];

}

1 个答案:

答案 0 :(得分:2)

也许是这样的:

修改

$seasonalGreetingA=array();
$seasonalGreetingA[]=array('dayBegin'=>30,'monthBegin'=>12,'dayEnd'=>31,'monthEnd'=>12,'text'=>'Happy New Year');
$seasonalGreetingA[]=array('dayBegin'=>1,'monthBegin'=>1,'dayEnd'=>2,'monthEnd'=>1,'text'=>'Happy New Year');
$seasonalGreetingA[]=array('dayBegin'=>21,'monthBegin'=>6,'dayEnd'=>23,'monthEnd'=>9,'text'=>'Happy Spring');
$seasonalGreetingA[]=array('dayBegin'=>12,'monthBegin'=>11,'dayEnd'=>23,'monthEnd'=>11,'text'=>'Happy All');

$dateGreetingA=array();
$dateGreetingA[]=array('date'=>'2014-11-09','text'=>'Happy Birthday');

$timeGreetingA=array();
$timeGreetingA[]=array('timeBegin'=>8,'timeEnd'=>12,'text'=>'Morning');
$timeGreetingA[]=array('timeBegin'=>8,'timeEnd'=>12,'text'=>'Good morning');
$timeGreetingA[]=array('timeBegin'=>18,'timeEnd'=>23,'text'=>'Evening');
$timeGreetingA[]=array('timeBegin'=>18,'timeEnd'=>23,'text'=>'Good evening');
$timeGreetingA[]=array('timeBegin'=>23,'timeEnd'=>24,'text'=>'Time out');
$timeGreetingA[]=array('timeBegin'=>13,'timeEnd'=>18,'text'=>'Good afternoon');

$standardGreetingA[]=array();
$standardGreetingA[]=array('text'=>'Hello');
$standardGreetingA[]=array('text'=>'Howdy');
$standardGreetingA[]=array('text'=>'Hi');

$txtGreeting='';

$date=date('Y-m-d');
if($txtGreeting=='')
if(count($dateGreetingA)>0)
    foreach($dateGreetingA as $dgA)
    {
        if($dgA['date']==$date) 
        {
            $txtGreeting=$dgA['text'];
            break;
        }
    }

$d=(int)date('d');
$m=(int)date('m');
if($txtGreeting=='')
if(count($seasonalGreetingA)>0)
    foreach($seasonalGreetingA as $sgA)
    {
        $d1=$sgA['dayBegin'];
        $m1=$sgA['monthBegin'];

        $d2=$sgA['dayEnd'];
        $m2=$sgA['monthEnd'];

        //echo $m1.' >= '.$m.' <= '.$m2.'<br />';
        if($m>=$m1 and $m<=$m2)
            if($d>=$d1 and $d<=$d2)
                $txtGreeting=$sgA['text'];

    }    

$time=(int)date('H');
if($txtGreeting=='')
if(count($timeGreetingA)>0)
    foreach($timeGreetingA as $tgA)
    {        
        if($time>=$tgA['timeBegin'] and $time<= $tgA['timeEnd']) 
        {            
            $txtGreeting=$tgA['text'];
            break;
        }
    }

if($txtGreeting=='')
    if(count($standardGreetingA)>0)
    {
        $ind=rand(0,count($standardGreetingA)-1);
        if(isset($standardGreetingA[$ind])) $txtGreeting=$standardGreetingA[$ind]['text'];
    }

echo $txtGreeting;
exit;