0

I wrote the following, however it doesn't seem to work.

What am I missing here?

Thank you

$ora_tora = date('H:i', strtotime('+2 hours')); 
     if (($ora_tora >= '00:00') && ($ora_tora < '02:00')) { echo '1'; }
else if (($ora_tora >= '02:00') && ($ora_tora < '04:00')) { echo '2'; }
else if (($ora_tora >= '04:00') && ($ora_tora < '06:00')) { echo '3'; }
else if (($ora_tora >= '06:00') && ($ora_tora < '08:00')) { echo '4'; }
else if (($ora_tora >= '08:00') && ($ora_tora < '10:00')) { echo '5'; }
else if (($ora_tora >= '10:00') && ($ora_tora < '12:00')) { echo '6'; }
else if (($ora_tora >= '12:00') && ($ora_tora < '14:00')) { echo '7'; }
else if (($ora_tora >= '14:00') && ($ora_tora < '16:00')) { echo '8'; }
else if (($ora_tora >= '16:00') && ($ora_tora < '18:00')) { echo '9'; }
else if (($ora_tora >= '18:00') && ($ora_tora < '20:00')) { echo '10'; }
else if (($ora_tora >= '20:00') && ($ora_tora < '22:00')) { echo '11'; }
else if (($ora_tora >= '22:00') && ($ora_tora < '00:00')) { echo '12'; }
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • 2
    Works for me... how does it not work? https://3v4l.org/Pj6X8 and https://3v4l.org/r3Dg2 – user3783243 Mar 13 '19 at 21:11
  • Possible duplicate of [PHP: How to compare a time string with date('H:i')?](https://stackoverflow.com/questions/10497929/php-how-to-compare-a-time-string-with-datehi) – Obsidian Age Mar 13 '19 at 21:11
  • Other than that you shouldn't have `&& ($ora_tora < '00:00')` in your last `else if` (which to my preference would be `elseif` not `else if`) your code should work fine. – Nick Mar 13 '19 at 21:12
  • 3
    if all you care about is the hour, why not just get `date('H', ...)`, convert to an int, divide by 2 and add 1? – solarc Mar 13 '19 at 21:13
  • @user3783243 if i change it to `date('h:i', strtotime('+2 hours'));` instead of `date('H:i', strtotime('+2 hours'));` then it works, but for AM. However, I want 24h – EnexoOnoma Mar 13 '19 at 21:16

2 Answers2

1

You could replace all of that with this:

echo intdiv(date('G', strtotime('+2 hours')), 2) + 1;

'G' is the format for 24 hours without leading 0 (so from 0 to 23)

solarc
  • 5,638
  • 2
  • 40
  • 51
  • Why do you use `intval` and `int` on the same line? – Dharman Mar 13 '19 at 21:28
  • @Dharman just a personal quirk. I use `intval` for converting strings and cast to int to convert from float to int. But right, it could just be removed. Done. – solarc Mar 13 '19 at 21:31
  • 1
    Ok, better, but still you have one `(int)` left. Why not just do `intdiv()`? Then you do not need to worry about it being a float. – Dharman Mar 13 '19 at 21:42
  • @Dharman `intdiv` was only introduced in PHP7, this is more generally useful. – Nick Mar 14 '19 at 02:54
  • 1
    @Nick What is more useful is giving people the push to upgrade to PHP 7, if they haven't done so already. You can't cling on to the past, PHP 5 is dead, move on... – Dharman Mar 14 '19 at 19:30
  • @Dharman sometimes upgrading is not something you have any control over... – Nick Mar 14 '19 at 21:48
0

since you actually compare hours you can do:

$ora_tora = (int)date('G', strtotime('+2 hours'));
if ($ora_tora > 2 && $ora_tora < 4) {

}
Markownikow
  • 457
  • 1
  • 4
  • 14