Reputation:
I have dates stored in an array but defining date ranges in a special format like this :
Array
(
[0] => Array
(
[0] => Array ///date start
(
[0] => 2017
[1] => 10 //month
[2] => 3 //day
)
[1] => Array //date end
(
[0] => 2017
[1] => 10 //month
[2] => 5 //day
)
)
[1] => Array
(
[0] => Array //date start
(
[0] => 2017
[1] => 11
[2] => 23
)
[1] => Array //date end
(
[0] => 2017
[1] => 11
[2] => 25
)
)
)
And I would need a function that can return if a specific string date exist in the array
2017-10-01. is_in_array ('2017-10-01').
But I don't understand how can I do with the foreach and the special array format of the date ranges.
Upvotes: 2
Views: 2779
Reputation: 16
If you need time in the mix you can try this variant
<?php
$discountDatesArr = [
[[2018, 8, 5, '00:00:00'], [2018, 8, 9, '23:59:59']],
[[2018, 8, 15, '00:00:00'], [2018, 8, 15, '23:59:59']],
[[2018, 8, 19, '00:00:00'], [2018, 8, 19, '23:59:59']],
[[2018, 8, 22, '00:00:00'], [2018, 8, 22, '23:59:59']],
[[2018, 8, 29, '00:00:00'], [2018, 8, 29, '23:59:59']],
];
$isDiscountSystemActive = false;
$dateUnix = strtotime(date("Y-n-j H:i:s"));
foreach ($discountDatesArr as $range) {
$rangeStartUnix = strtotime("{$range[0][0]}-{$range[0][1]}-{$range[0][2]} {$range[0][3]}");
$rangeEndUnix = strtotime("{$range[1][0]}-{$range[1][1]}-{$range[1][2]} {$range[1][3]}");
if ($dateUnix >= $rangeStartUnix && $dateUnix < $rangeEndUnix) {
$isDiscountSystemActive = true;
}
}
define ("ISDISCOUNTACTIVE", $isDiscountSystemActive);
echo ISDISCOUNTACTIVE;
Upvotes: 0
Reputation: 2154
Try this
$arr = array(
[
[2017,10,3],
[2017,10,5]
],
[
[2017,11,23],
[2017,11,25]
],
[
[2017,12,1],
[2017,12,10]
]
);
function is_in_array($array, $date) {
$timestamp = strtotime($date);
$date = date('d',$timestamp);
$month = date('m',$timestamp);
$year = date('Y',$timestamp);
foreach ($array as $key => $value) {
foreach($value as $value2) {
if($value2[0]==$year && $value2[1] == $month && $date == $value2[2])
return true;
}
}
return false;
}
Here first parameter to is_in_array()
is an array from which you want to find date
and second parameter is date that you are looking for.
Therefore
is_in_array('2017-12-1'); //will return true
is_in_array('2017-5-2'); //will return false
Upvotes: 1
Reputation: 4375
Try this:
$rangesArr = [
[[2017, 10, 3], [2017, 10, 5]],
[[2017, 11, 23], [2017, 11, 25]],
];
function is_in_array ($date) {
global $rangesArr;
$dateUnix = strtotime($date);
foreach ($rangesArr as $range) {
$rangeStartUnix = strtotime("{$range[0][0]}-{$range[0][1]}-{$range[0][2]}");
$rangeEndUnix = strtotime("{$range[1][0]}-{$range[1][1]}-{$range[1][2]}");
if ($dateUnix >= $rangeStartUnix && $dateUnix < $rangeEndUnix) {
return true;
}
}
return false;
}
var_dump(is_in_array('2017-10-04')); // Returns true.
var_dump(is_in_array('2017-10-05')); // Returns false.
Output:
bool(true)
bool(false)
Basically, all it does is loop through the array, convert the sub range start and end arrays into unix timestamps, and compares that with the unix timestamp of the date that was passed in.
Upvotes: 0