我有以下数组:
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 ) )
我想计算所选年份之间的所有日期,例如BETWEEN 1970/01/01和1980/01/01应为1.我已尝试以下各项:
foreach($data as $item){
if ($item['dob'] == ('>=' . '1970-01-01' && '<' . '1980-01-01'))
{
$a++;
}
然后我将变量传递给数组然后转换为JSON数组以填充图表,但我得到以下输出:
{"1970s":30,"1980s":0,"1990s":0}
请问这是什么语法?感谢
答案 0 :(得分:0)
它将您的日期传递给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++;
}
?>
答案 1 :(得分:0)
您可以使用array_filter和DateTime来获得所需内容:
<?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);
答案 2 :(得分:0)
或简单的字符串操作/数学舍入解决方案
$result = [];
foreach($data as $item) {
$decade = ((int)(substr($item['dob'], 0, 4) / 10)) * 10;
$result[$decade . 's']++;
}
ksort($result);
echo json_encode($result);
答案 3 :(得分:0)
谢谢大家,解决方案非常简单,因为我只需要计算每个结果,所以我使用了以下内容:
$_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);
这给出了以下输出:
{"1950s":1,"1960s":1,"1970s":4,"1980s":6,"1990s":5,"2000s":11,"2010s":2,"2020s":0}
然后将其分成标签/值对以填充morris.js圆环图。
答案 4 :(得分:0)
试试这个
<?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;
?>