如果是基于时间的陈述

时间:2015-11-19 13:55:05

标签: php

我正在尝试在PHP上创建一个代码,当上午6:00到下午5:59之间的时间是早上并且将在下午6:00到5:59之间显示太阳将显示月亮。

到目前为止,这是我的代码:

它始终显示月亮图像。

<?php
date_default_timezone_set("Asia/Manila");
    echo date("l, ");
    echo date("d F Y  ");


    echo date("G:i:sa  ");

    if (date('G'>17) && ( 'f' >=59) && ('sa' >=59))
    {
        echo $timeImage = "<img src=sun.png>";

    }

    else if(date('G'<6) && ('f' <= 01) && ('sa' <= 01 ))
    {
        echo $timeImage = "<img src=moon.png>";

    }


?>

1 个答案:

答案 0 :(得分:2)

<?php
$seconds = time() - strtotime("today"); //seconds since midnight for now
$sec_a = 6*60*60; //seconds since midnight for 6am
$sec_b = 18*60*60; //seconds since midnight for 6pm

if($seconds >= $sec_a and $sec_b >= $seconds) //current seconds are larger than 6am or equal but smaller than 6pm
{
    echo "daytime";
} else {
    echo "nighttime";
}