ISO8601 Format without Y-M-D - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Wednesday, December 8, 2021

ISO8601 Format without Y-M-D

I have a date formatted in ISO8601 that I want to use and compare against different times. For example if it's between 18:00 and 21:00, return true. Else return false. But I don't want to compare the Y-M-D portion of it. I want it to be any day, but those times.

public function compareTimes($time) {
        $dateTime = DateTime::createFromFormat("Y-m-d\TH:i:s+", $time);
        $begin = new DateTime('18:00');
        $end = new DateTime('22:00');
        if($dateTime >= $begin && $dateTime <= $end) {
            return true;
        } else {
            return false;
        }
    }

I have this code, however it compares it vs the Y-m-d as well so if it happened on a different day it wouldn't work. So I don't want to compare it against those 3 things. However if I ommit them, it's not a valid DateTime and won't work



source https://stackoverflow.com/questions/70265484/iso8601-format-without-y-m-d

No comments:

Post a Comment