How to use PHP to determine whether it is morning or afternoon: first set the Chinese time zone; then define a GetDateTime method; then use the if else statement to determine whether the current time is morning or afternoon; and finally output the judgment result.
Recommended: "PHP Video Tutorial"
Judge good morning and good afternoon based on the current time
header("Content-type:text/html;charset=utf-8"); date_default_timezone_set('PRC'); //设置中国时区 echo str_replace(array('AM','PM'),array('上午','下午'),date("Y-m-d A H:i:s"));
function GetDateTime(){ $Datetime = date('H'); $text = ""; if($Datetime >= 0 && $Datetime < 7){ $text = "天还没亮,夜猫子,要注意身体哦! "; }else if($Datetime>=7 && $Datetime<12){ $text = "上午好!今天天气真不错……哈哈哈,不去玩吗?"; }else if($Datetime >= 12 && $Datetime < 14){ $text = "中午好!午休时间哦,朋友一定是不习惯午睡的吧?!"; }else if($Datetime >= 14 && $Datetime < 18){ $text = "下午茶的时间到了,休息一下吧! "; }else if($Datetime >= 18 && $Datetime < 22){ $text = "下午茶的时间到了,休息一下吧! "; }else if($Datetime >= 22 && $Datetime < 24){ $text = "很晚了哦,注意休息呀!"; } return $text; }
The above is the detailed content of How to determine morning or afternoon in php. For more information, please follow other related articles on the PHP Chinese website!