Reputation: 101
I have an array like
$array = (
0 => array(
"start" => "Mon 09:30",
"end" => "Mon 11:00"
) ,
1 => array(
"start" => "Sun 14:10",
"end" => "Sun 20:00"
) ,
array(
"start" => "Sun 07:30",
"end" => "Sun 08:00"
));
Is there any idea to perform a function only if the current time is within this time.
If the current time is between start and end echo or return true. Date is not important
Upvotes: 0
Views: 303
Reputation: 47991
Creating datetime objects for each value in the time range subarrays is unnecessary overhead for your task. strtotime()
gives you what you need and only what you need.
As a matter of best practice, you should perform an early return as soon as you find a qualifying time range -- so your code is not performing wasteful iterations.
Code: (Demo)
$array = [
["start" => "Mon 09:30", "end" => "Mon 11:00"],
["start" => "Sun 14:10", "end" => "Sun 20:00"],
["start" => "Sun 07:30", "end" => "Sun 08:00"],
["start" => "Sun 08:30", "end" => "Sun 09:30"]
];
function in_range($array) {
$now = time(); // using server timezone
echo date("D H:i") , "\n";
foreach ($array as $range) {
if ($now >= strtotime($range["start"]) && strtotime($range["end"]) >= $now) {
return $range; // or true if you like
}
}
return false;
}
var_export(in_range($array));
Output (at the moment):
Sun 09:02
array (
'start' => 'Sun 08:30',
'end' => 'Sun 09:30',
)
If it is an impossibility for different daynames to occur in the same row of data, then you can further optimize your lookup process by removing the redundancies in your lookup array.
If you restructure your day-time rows to be grouped by dayname and use that value as your associative first level key, you can use isset()
to offer a quick return without any time comparisons and eliminate the chance of uselessly iterating all other days of the week.
Code: (Demo)
$array = [
"Mon" => [
["start" => "09:30", "end" => "11:00"]
],
"Sun" => [
["start" => "07:30", "end" => "08:00"],
["start" => "08:30", "end" => "09:30"],
["start" => "14:10", "end" => "23:59"]
]
];
function in_range($array) {
$dayname = date("D");
if (!isset($array[$dayname])) {
return false; // quick, 1st level return
}
$now = date("H:i");
foreach ($array[$dayname] as $range) {
if ($now >= $range["start"] && $range["end"] >= $now) {
return true; // quick return
}
}
return false; // fallback return after iterating the dayname group
}
var_export(in_range($array));
This may be premature optimization. I don't know the size and scope of your project. I just wanted to mention this in case a future researcher's project could benefit from it.
Upvotes: 1
Reputation: 4389
Here is a solution, however, don't forget to adjust your network timezone. You don't even need any strtotime() in fact.
<?php
$dateRanges = array(
0 => array(
"start" => "Mon 09:30",
"end" => "Mon 11:00"
),
1 => array(
"start" => "Sun 14:10",
"end" => "Sun 20:00"
),
2 => array(
"start" => "Sun 07:30",
"end" => "Sun 08:00"
)
);
function checkIfItIsNow($dateRanges){
$now = date('D H:i');
$result = false;
foreach ($dateRanges as $range) {
if(($now > $range['start']) && ($now < $range['end'])){
$result = true;
break;
}
}
return $result;
}
$finalResult = checkIfItIsNow($dateRanges);
var_dump($finalResult);
?>
Let me know if you have any question.
Upvotes: 2