questions

Setting time with minutes using date ();

Rate this post

So I have this code that works perfectly:

<?php
    $time = date("H");
    $timezone = date("e");
    if ($time < "13") {
        echo "message1";
    } else
    if ($time >= "13" && $time < "19") {
        echo "message2";
    } else
    if ($time >= "19" && $time < "24") {
        echo "message3";
    }
    ?>

The problem is that I can only use hours: 1, 5, 10, 19 and etc.

I need to use minutes as well, eg. 13:30.

I have tried changing to 13:30. Also tried changing to 1350.

But can’t make it work.

Need help.

 

✔️Solution:

Create a new variable that hold the current minute,

DateTime::format shows us that date("i") gives:

Minutes with leading zeros

With the current hour and minute, you can calculate any time, for example:

<?php
    $hour = date("H");
    $minute = date("i");
    
    // Between 13:30 and 13:30
    if ($hour === 13 && $minute <= 30) {
        echo "message1";
    } 

    // Between 13:30 and 19:00
    elseif (($hour >= 13 && $minute >= 30) && $hour < 19) {
        echo "message2";
    } 

    // After 19:00
    else {
        echo "message3";
    }
    ?>

Leave a Reply

Your email address will not be published.

Back to top button