4
\$\begingroup\$

The working hours are saved in Database like

{"1" : "11:00 - 14:30", "2" : "17:30 - 23:00"}

Now i wrote a function like, which test is the shop is opened or close at the moment. Can someone please suggest if there is a better way to do this? And how i can check if the timing are like

{"1" : "11:00 - 14:30", "2" : "17:30 - 02:00"}

Where 02:00 o clock is next day's time. here is my funciton.

$working_hours = json_decode('{"1" : "11:00 - 14:30", "2" : "14:39 - 23:00"}');
$is_open = false;
$time = time();

foreach ($working_hours as $key=>$value)
{
   $working_hour = explode(" - ", $value);
   $from = strtotime($working_hour[0]);
   $to = strtotime($working_hour[1]);

   if ($time >= $from && $time <= $to)
   {
       $is_open = true;
       break;
   }    
}

echo "\n***********************";
echo "\n". date("d.m.Y H:i", $time);

if($is_open)
   echo "\nOpend";
else
   echo "\nClosed";

echo "\n***********************";
\$\endgroup\$
  • \$\begingroup\$ OK, you've convinced me that NoSQL is a really bad idea. \$\endgroup\$ – symcbean Mar 5 '13 at 21:07
  • \$\begingroup\$ how it is bad idea :) And how I can do it better in Many to Many relations \$\endgroup\$ – user22818 Mar 6 '13 at 8:30
2
\$\begingroup\$

Easiest solution (to the time problem) is to change your logic (just a tiny bit).

if ($time >= $from && $time <= $to)

To:

if (($to > $from && ($time >= $from && $time <= $to)) || 
    ($to < $from && ($time >= $from || $time <= $to)))

You just change whether to restrict the time to be between the two, or to restrict it to be greater than one or less than the other in the case that the $to time is less than $from.


As far as cleaning up your code: this is about as idiomatic as will get. Though you should definitely extract this to a function as appropriate.

\$\endgroup\$
1
\$\begingroup\$

If 02:00 consider the next day, then you'll need to decide on some cut-off for what's considered 'today' and what's considered 'tomorrow'.

\$\endgroup\$
  • \$\begingroup\$ Yes i think if $to < $from then $to should be considered as tomorrow. \$\endgroup\$ – user22818 Mar 5 '13 at 15:31
1
\$\begingroup\$

I would do some preparation, that will convert this

{"1" : "11:00 - 14:30", "2" : "17:30 - 02:00"}

to this

{"1" : "00:00 - 02:00", "2" : "11:00 - 14:30", "3" : "17:30 - 23:59"}

E.g. this way (code wasnt run or tested):

$working_hours = json_decode('{"1" : "11:00 - 14:30", "2" : "14:39 - 23:00"}');
$last_key = count($working_hours); // make it better if keys are more random

// parse to\from as decimal hour values, there could be a better way
list($from, $to) = explode(" - ", $working_hours[$last_key]);
$from_arr = explode($from,":");
$from_dec = $from_arr[0] + $from_arr[1]/60;
$to_arr = explode($to,":");
$to_dec = $to_arr[0] + $to_arr[1]/60;

// if the last period has "strange" order
if ($to_dec < $from_dec)
{
    // cut the last period with 24:00
    $working_hours[$last_key] = $from_arr[0].":".$from_arr[1]." - 23:59";
    // add new period for early morning
    array_unshift($working_hours, "00:00 - ".$to_arr[0].":".$to_arr[1]); // you dont have to push it to 1st position, can also add it to the end of array
}

$is_open = false;
$time = time();
... // your code follows

Anyway looks even more ugly. But i'm affraid there is no way to handle wrond data structures a pretty way ))

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.