2

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.

3
  • So, what if you had a range like [[2017, 10, 3], [2017, 10, 5]] and you wanted to check the date 2017-10-04, should it find it or not? Commented Nov 2, 2017 at 14:51
  • the function returns true Commented Nov 2, 2017 at 14:54
  • but the end date is not included, so for 2017-10-05, function should returns false Commented Nov 2, 2017 at 15:03

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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;

https://eval.in/1044922

Comments

0

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.

eval.in demo

4 Comments

thanks a lot i will check this !! is there any difference in between 2017-10-04 and 2017-10-4 ?
thanks , is your script compatible with the multidimensional array described in my post because for the moment the function always returns false...
@Pipoo make sure you updated global $rangesArr; and foreach ($rangesArr as $range) { to the correct array name. Yes it's compatible, it's the exact same format, just condensed.
yes i pasted your function , but if you tell that $rangesArr is the same than my array, it must be my mistake, i am checking it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.