如何在php H格式编程半小时



我在stackoverflow和google上寻找类似的问题,但没有找到适合我问题的答案。我仍然很抱歉,如果有任何机会我让一个重复的问题通过。如果你能告诉我,我会很感激的。

我的问题是我需要在php中编程一个以H格式格式化的时间表,但现在我需要添加半小时,不知道如何。下面是我的代码,这样你可以更好地理解我的意思:

$time = (int)date('H');
if($time>=24 && $time<6)
{
    //some code...
}
else if($time>=6 && $time<18) //My problem is here, I need it to be 18:30
{
    //some other code...
}
//code goes on for rest of hours of the day

我尝试将$time更改为(int)date('H:i'),但是当我写if($时间> = 6点,,美元time<18:00)我得到一个错误,因为':'

EDIT: ok,我试了两种方法,这是代码的样子

    date_default_timezone_set('America/Lima');
    if (!isset($timestamp)) {
        $timestamp = time();
    }
    $dw = date( "w", @$timestamp);
    $time = date('HH:ii');
    if($dw>0 && $dw<=4)
    {
        if($time>="00:00" && $time<"04:00")
        {
            $show->current = "A";
            $show->next= "B";
        }
    else if($time>="04:00" && $time<"06:30")
    {
    $show->current = "B";
            $show->next= "C";   
    }
        else if($time>="06:30" && $time<"09:00")
        {
            $show->current = "C";
            $show->next= "D";
        }

继续,直到再次到达00:00。问题是这种方式似乎不能识别所有的日期,所以如果例如我编辑当前C时间为06:00而不是06:30,它不会在我的网站上更新。

    date_default_timezone_set('America/Lima');
    if (!isset($timestamp)) {
        $timestamp = time();
    }
    $dw = date( "w", @$timestamp);
    $time = date('G');

    if($dw>0 && $dw<=4)
       {
        if($time>=0 && $time<4)
        {
            $show->current = "A";
            $show->next= "B";
        }
    else if($time>=4 && $time<6.5)
    {
    $show->current = "B";
            $show->next= "C";   
    }
        else if($time>=6.5 && $time<9)
        {
            $show->current = "C";
            $show->next= "D";
        }

这里它更新得很好,但是它不能识别6.5为6:30,所以在那个时候,我不是得到当前的C显示,而是继续得到b

如果您想直接比较半小时的时间,您必须将hh:mm转换为数字格式,例如

# 6:45
$hours = 6 + (45 /60); # represent as fractional hours
$minutes = (6 * 60) + 45; # represent in minutes only.

,然后你可以直接比较:

#    6am            6:30pm
if (($hours > 6) && ($hours <= 18.5))
if (($minutes > 360) && ($minutes < 1125))

您正在将$time转换为整数,这就是为什么它不起作用。
试一试:

$time = date('H:i');     // <-- without the "(int)" part
if (($time >= "00:00") && ($time < "06:00")) {
    //some code...
} else if (($time >= "06:00") && ($time < "18:30") {
    //some other code...
} else {
    //code goes on for rest of hours of the day
}

编辑:
请确保在0到9之间的小时前加上一个零,例如用06:...代替6:...
(感谢Marc B的提醒)。

EDIT2 :根据文档关于Hi格式字符:

#    format
# character   Description                                    Returned values 
#         H   24-hour format of an hour with leading zeros   00 through 23
#         i   Minutes with leading zeros                     00 to 59

因此,期望$time中从00:0023:59的值(而不是例如24:00)

H只返回小时。您需要使用$minutes = date('i')获得一个单独的分钟值。使用

将$minutes添加到elseif中
if($time>=6 && ($time<19 && $minutes < 30) )

编辑:正如Marc B所指出的,如果$time大于6,小于19,但是minutes大于30,例如,7:45(一个有效的时间),这个方法将失败。

解决方案应按照Expert System建议。使用date("Hi")或(int) date("Gi");

如果希望使用$minutes,则应进一步添加OR子句。比如

if( $time>=6 && ($time<18 || ($time == 18 && $minutes < 30)) )

最新更新