How can I create a foreach loop that will increase a variable for ranges between 2 dates?

I have the following array:

Array ( [0] => Array ( [dob] => 1990-01-01 )
        [1] => Array ( [dob] => 1979-10-01 ) 
        [2] => Array ( [dob] => 1982-05-07 ) 
        [3] => Array ( [dob] => 1967-08-01 ) 
        [4] => Array ( [dob] => 1967-10-01 )
        [5] => Array ( [dob] => 1965-03-01 )
        [6] => Array ( [dob] => 1970-03-03 )
        [7] => Array ( [dob] => 1977-01-03 ) 
        [8] => Array ( [dob] => 1993-10-06 ) 
        [9] => Array ( [dob] => 1991-09-10 ) 
        [10] => Array ( [dob] => 1994-02-02 ) 
        [11] => Array ( [dob] => 1994-08-03 ) 
        [12] => Array ( [dob] => 1994-08-03 ) 
        [13] => Array ( [dob] => 1997-04-03 ) 
        [14] => Array ( [dob] => 1997-04-03 ) 
        [15] => Array ( [dob] => 1997-04-03 ) 
        [16] => Array ( [dob] => 1997-04-03 ) 
        [17] => Array ( [dob] => 1947-02-16 ) 
        [18] => Array ( [dob] => 1957-10-17 ) 
        [19] => Array ( [dob] => 1980-01-03 ) 
        [20] => Array ( [dob] => 1985-04-04 ) 
        [21] => Array ( [dob] => 1998-02-03 ) 
        [22] => Array ( [dob] => 1969-06-06 ) 
        [23] => Array ( [dob] => 2000-03-03 ) 
        [24] => Array ( [dob] => 1980-04-02 ) 
        [25] => Array ( [dob] => 1973-03-03 ) 
        [26] => Array ( [dob] => 1984-06-14 ) 
        [27] => Array ( [dob] => 1976-10-17 ) 
        [28] => Array ( [dob] => 2003-03-01 ) 
        [29] => Array ( [dob] => 1972-05-05 ) ) 

I want to count all dates between selected years, for example BETWEEN 1970/01/01 and 1980/01/01 should be 1. I have tried the following for each:

foreach($data as $item){
    if ($item['dob'] == ('>=' . '1970-01-01' && '<' . '1980-01-01'))
    {
        $a++;
    }

I then pass the variables to an array then convert to a JSON array to populate a chart, but im getting the following output:

{"1970s":30,"1980s":0,"1990s":0}

What is the syntax to do this please? Thanks

It pass your dates to strtotime,

<?php
$startDate = '1970-01-01';
$endDate = '1980-01-01';
foreach($data as $item){
    if ((strtotime($item['dob'])>=strtotime($startDate)) && (strtotime($item['dob'])<strtotime($endDate)))
    {
        $a++;
    }
?>

You can use array_filter and DateTime to get what you want :

<?php

/**
 *    @param array $data a data value
 *    @param string $from the from date in Y-m-d format
 *    @param string $to the to date in Y-m-d format
 *    @return true if data is between the range, false otherwise
 */
function filter_by_date($data, $from, $to)
{
    $date = DateTime::createFromFormat('Y-m-d', $data['dob']);
    $from = DateTime::createFromFormat('Y-m-d', $from);
    $to   = DateTime::createFromFormat('Y-m-d', $to);

    if (false === $date || false === $from || false === $to) {
        throw new /RuntimeException('Dates are not in Y-m-d format');
    }

    return $date >= $from && $date < $to;
}

function filter_80s($data)
{
    return filter_by_date($data, '1980-01-01', '1990-01-01');
}

function filter_70s($data)
{
    return filter_by_date($data, '1970-01-01', '1980-01-01');
}

$datas = array(
    array('dob' => '1990-01-01'),
    array('dob' => '1979-10-01'),
    array('dob' => '1982-05-07'),
    array('dob' => '1967-08-01'),
    array('dob' => '1967-10-01'),
);

$chartDatas = array(
    '1970s' => count(array_filter($datas, 'filter_70s')),
    '1980s' => count(array_filter($datas, 'filter_80s')),
);

echo json_encode($chartDatas);

or a simple string manipulation/math rounding solution

$result = [];
foreach($data as $item) {
    $decade = ((int)(substr($item['dob'], 0, 4) / 10)) * 10;
    $result[$decade . 's']++;
}
ksort($result);
echo json_encode($result);

Thanks all, the solution was very simple as I only need a count of each result so I used the following:

$_1950s = 0;
$_1960s = 0;
$_1970s = 0;
$_1980s = 0;
$_1990s = 0;
$_2000s = 0;
$_2010s = 0;
$_2020s = 0;
$misc = 0;
foreach($data as $item){
    if ($item['dob'] >= '1940-01-01' && $item['dob'] < '1950-01-01'){
        $_1950s++;
    }
    elseif ($item['dob'] >= '1950-01-01' && $item['dob'] < '1960-01-01'){
        $_1960s++;
    }
    elseif ($item['dob'] >= '1960-01-01' && $item['dob'] < '1970-01-01'){
        $_1970s++;
    }
    elseif ($item['dob'] >= '1970-01-01' && $item['dob'] < '1980-01-01'){
        $_1980s++;
    }
    elseif ($item['dob'] >= '1980-01-01' && $item['dob'] < '1990-01-01'){
        $_1990s++;
    }
    elseif ($item['dob'] >= '1990-01-01' && $item['dob'] < '2000-01-01'){
        $_2000s++;
    }
    elseif ($item['dob'] >= '2000-01-01' && $item['dob'] < '2010-01-01'){
        $_2010s++;
    }
    elseif ($item['dob'] >= '2010-01-01' && $item['dob'] < '2020-01-01'){
        $_2020s++;
    }
    else{
        $misc++;
    }
}
$res = array("1950s"=>$_1950s, "1960s"=>$_1960s, "1970s"=>$_1970s, "1980s"=>$_1980s, "1990s"=>$_1990s, "2000s"=>$_2000s, "2010s"=>$_2010s, "2020s"=>$_2020s);
$data = json_encode($res);

This gave the following output:

{"1950s":1,"1960s":1,"1970s":4,"1980s":6,"1990s":5,"2000s":11,"2010s":2,"2020s":0}

this is then seperated into Label / Value pairs to populate a morris.js donut chart.

try this

<?php
    $start_date=strtotime("1970-01-01");
    $end_date=strtotime("1980-01-01");
    $count=0;
    foreach($date as $date){
        if(strtotime($date)>=$start_date && strtotime($date)<=$end_date) $count+=1;
    }
    echo $count;
?>