1

I have a function that checks if the current time is within the opening time. My problem is that I am unsure if it is the right approach.

The function accepts the following parameters: string $range and string $now.

$range = "08:00-12:00";
$now = "09:33";

function isOpen(string $range, string $now): bool {}

My approach: I convert each individual time to a number. (hours * 60) + minutes. This way I can compare the times. It works and I have not found any problems in the current tests. There are, as so often, several approaches. I would be interested to know which approach you would choose?

Thanks in advance!!

Maik Lowrey
  • 10,972
  • 4
  • 14
  • 43
  • Are the timestamps just generic hour:minute like shown? If they're full on dates, I'd convert them into `DateTime` objects, which can be compared directly against each other. – Alex Howansky Dec 11 '21 at 17:25
  • @AlexHowansky It is only hours:minutes and in string format. I think that is also sufficient for DateTime. – Maik Lowrey Dec 11 '21 at 17:37
  • 2
    It'sway more complicated that it looks. You could work using epoch integers and the spaceship operator https://stackoverflow.com/questions/30365346/what-is-the-spaceship-operator-in-php-7 – NVRM Dec 11 '21 at 17:55
  • If it works for you, then use it – Justinas Dec 11 '21 at 20:06
  • 1
    @Justinas Don't be afraid. I use it. Aren't you allowed to ask for alternatives? Then you don't have to downvote if someone asks. Can you explain your downvote to me or is it no longer allowed to ask other developers? – Maik Lowrey Dec 11 '21 at 20:13
  • The downvote was likely bcs this question is not suitable for SO, as it is asking for opinions on different appraches. From the help: "*Opinion-based - discussions focused on diverse opinions are great, but they just don't fit our format well. This question is likely to be answered with opinions rather than facts and citations. It should be updated so it will lead to fact-based answers.*" See [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) and [What types of questions should I avoid asking?](https://stackoverflow.com/help/dont-ask) guides for more info. – Don't Panic Dec 11 '21 at 22:55
  • @MaikLowrey IF there is at least 2 alternatives, then you can't reliably select _best_ answer. That's why that kind of questions is off topic for SO. So yes, you can't ask for alternatives – Justinas Dec 12 '21 at 07:46

0 Answers0