根据一天中的时间运行代码 [英] run code depending on the time of day

查看:70
本文介绍了根据一天中的时间运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据一天中的小时在页面上回显一些代码或消息.就像一个欢迎消息,晚上好"或下午好"

I need to echo some code or message on a page depending on the hour of the day. Just like a welcome message, "good evening", or "good afternoon"

我不知道如何分组某些小时并像这样向每个分组分配一条消息

I don't know hot to group certain hours and assign a message to each group like

从1:00:00 pm到4:00:00 pm =下午好"和从4:00:01到8:00:00 =晚上好"

from 1:00:00 pm to 4:00:00pm = "good afternoon" and from 4:00:01 to 8:00:00 = "good evening"

到目前为止,我有:

<?php
date_default_timezone_set('Ireland/Dublin');
$date = date('h:i:s A', time());
if ($date < 05:00:00 AM){
echo 'good morning';
}
?>

但是我不知道如何传递消息的时间范围.

But I don't know how to pass the hours range an the messages.

推荐答案



    <?php
    /* This sets the $time variable to the current hour in the 24 hour clock format */
    $time = date("H");
    /* Set the $timezone variable to become the current timezone */
    $timezone = date("e");
    /* If the time is less than 1200 hours, show good morning */
    if ($time < "12") {
        echo "Good morning";
    } else
    /* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */
    if ($time >= "12" && $time < "17") {
        echo "Good afternoon";
    } else
    /* Should the time be between or equal to 1700 and 1900 hours, show good evening */
    if ($time >= "17" && $time < "19") {
        echo "Good evening";
    } else
    /* Finally, show good night if the time is greater than or equal to 1900 hours */
    if ($time >= "19") {
        echo "Good night";
    }
    ?>

这篇关于根据一天中的时间运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆