0

I made this switch, but I dont know this last step (the ?? and the comments). If someone can help me, thanks a lot!

<?
   $nuDatumTijd = date("Y-m-d H:i:s");
   $nuUur = date("H");
   switch(true)
   {
      case $nuUur > 8 && $nuUur < 13:
      $aantSecErbij = ??; // number of seconds untill the first next 13:00
      $weerOp = date($nuDatumTijd,$aantSecErbij);
      echo $weerOp;
      break;
      
      case $nuUur > 12 && $nuUur < 18:
      $aantSecErbij = ??; // number of seconds untill the first next 19:00
      $weerOp = date($nuDatumTijd,$aantSecErbij);
      echo $weerOp;
      break;
      
      case $nuUur > 17 && $nuUur < 22:
      $aantSecErbij = ??; // number of seconds untill the first next 9:00 (so that is the next day)
      $weerOp = date($nuDatumTijd,$aantSecErbij);
      echo $weerOp;
      break;
   }
?>
      
      
Erik
  • 85
  • 8
  • 1
    These are invalid switch case statements - use else if statements instead (https://www.php.net/manual/en/control-structures.switch.php) –  Feb 25 '21 at 10:12
  • 1
    This should slove the core issue for you: [date difference in seconds](https://stackoverflow.com/questions/1519228/get-interval-seconds-between-two-datetime-in-php/17114475). – El_Vanja Feb 25 '21 at 10:20
  • Thank you Jeff, I can re-write it to else/if. But the question about $aantSecErbij stays. – Erik Feb 25 '21 at 10:21
  • This line $weerOp = date ($nuDatumTijd, $aantSecErbij); is nonsense. What do you expect for $weerOp. Please give an example. – jspit Feb 25 '21 at 11:26

2 Answers2

1

I now have fixed it this way, thanks for your input:

$nuUur = date("H");

        if ($nuUur > 8 && $nuUur < 13)
        {
            $weerOp = strtotime('today 13:00');
            $weerOpNetjes = date('Y-m-d H:i:s', $weerOp);
            echo $weerOpNetjes;
        }
        elseif ($nuUur > 12 && $nuUur < 18)
        {
            $weerOp = strtotime('today 19:00');
            $weerOpNetjes = date('H:i', $weerOp);
            echo $weerOpNetjes;
        }
        elseif ($nuUur > 17 && $nuUur < 22)
        {
            $weerOp = strtotime('tomorrow 9:00');
            $weerOpNetjes = date('H:i', $weerOp);
            echo $weerOpNetjes;
        }
Erik
  • 85
  • 8
0

switch-case can be used this way too, no problem about that.

$aantSecErbij = ??; // number of seconds untill the first next 13:00

If this means the number of seconds until today's 1pm then the following will get you the timestamp of today's 1pm.

$now = getdate();
$time1pm = mktime(13, 0, 0, $now['mon'], $now['mday'], $now['year']);

If it means the number of seconds between now and today's 1pm then you need to subtract with current timestamp.

$now = getdate();
$diff1pm = mktime(13, 0, 0, $now['mon'], $now['mday'], $now['year']) - time();

.

$aantSecErbij = ??; // number of seconds untill the first next 9:00 (so that is the next day)

For the next day you can add 1 to the day part and don't have to worry about the end of month. PHP will figure out the right date for you.

$now = getdate();
$tomorrow9am = mktime(9, 0, 0, $now['mon'], $now['mday'] + 1, $now['year']);
Anurat Chapanond
  • 2,837
  • 3
  • 18
  • 31