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

How to determine morning or afternoon in php

藏色散人
Release: 2023-03-05 07:24:02
Original
4914 people have browsed it

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"));
Copy after login
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;
    }
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template