Randomised Greeting Function - PHP -
so, have created script in php sets greeting dependent on date , time. code checks seasonal events, , if there none, checks time , assigns appropriate greeting. issue having is:
i have set value check if time past 18:00, if past time, should set eve greeting. issue if alter php if argument value time in future, still keeps greeting eve greeting. wrong?
here code:
$morninggreetings = array( 1 => "good morning", 2 => "morning", ); $standardgreetings = array( 1 => "hello", 2 => "howdy", 3 => "hiya", 4 => "greetings", 5 => "great 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]; }
maybe this:
edit
$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 $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 $sga) { $d1=$sga['daybegin']; $m1=$sga['monthbegin']; $d2=$sga['dayend']; $m2=$sga['monthend']; //echo $m1.' >= '.$m.' <= '.$m2.'<br />'; if($m>=$m1 , $m<=$m2) if($d>=$d1 , $d<=$d2) $txtgreeting=$sga['text']; } $time=(int)date('h'); if($txtgreeting=='') if(count($timegreetinga)>0) foreach($timegreetinga $tga) { if($time>=$tga['timebegin'] , $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;
php function date time
No comments:
Post a Comment