Home  >  Article  >  Backend Development  >  How to determine morning or afternoon in php

How to determine morning or afternoon in php

藏色散人
藏色散人Original
2020-08-24 10:50:024832browse

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.

How to determine morning or afternoon in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn