简体   繁体   中英

Code to Check if time is at or before 2pm PHP

I have a code inside of a switch statement that is supposed to check if a time entered is at or before 2pm. If it is, it continues in the loop that it is in, if not, it skips the loop it's in (The switch statement is inside of a foreach loop). Here is my current code:

case '3370':
            $dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sO', $_SESSION["apptTime"]);
    $timestamp = $dateTime->getTimestamp();
    $time = date('H', $timestamp);
    if($time <= 14){
        $addonimage = 'https://mywebsite.com/image.png';
        break;

    }else{
        continue 2;
}   

The code works for the most part, except when the time selected is for 2pm. I want it to create the $addonimage but instead, it goes to continue 2 . How can I make it do this?

Update:

OK, So I did a little troubleshooting and I found out that the hour being passed is 15, where it should be 14. Here is a sample time being passed into the function: 2017-03-11T14:00:00-0800 . Does anyone know why it's passing 15 instead of 14?

OK, I actually found something that works. Instead of using the date function, I just use the DateTime function. Here is what I have now:

$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sO', $_SESSION["apptTime"]);
    $timestamp = $dateTime->format('H');
    if(intval($timestamp) <= 14){

This does exactly what I want it to do. Thank you everyone for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM