Change the date after 6 hours in the php variable

advertisements

I need to change the shiftdate variable after 05:30 AM. Since i need to generate data from past 24 hrs starting 05:31 AM to Next day 05:30 AM. I tried like this, but its giving previous day every time. Please help.

I want $shiftdate to use in my sql query;

Code:

<?php
    if(date('H:i')>="00:00" || date('H:i')<"05:30"){
                      $shiftdate=  date('Y-m-d',strtotime(date('Y-m-d'))-24*60*60);

                  }
                  else if(date('H:i')>"05:30" || date('H:i')<"00:00")
                  {
                      $shiftdate=date('Y-m-d');
                  }

                  echo $shiftdate;
?>


  1. You can't just compare string like "05:30" as a number and hope for the best. You need to compare numerical value of the hour and then numerical value of the minute.

  2. You have a race in between the first if and the else if

  3. Also the else if doesn't cover it completely, so if it hit's the sweetspot, you can end up with $shiftdate being NULL.

  4. Make it a function with protoype shiftdate_type_whatever_it_is fn_name(int hour, int minute);. This way you can simply unit test the function for different (think boundary) values of the date("H:i");