如何过滤5月中created_at
所在的条目?
$data = [
(object) [
'name' => 'Gadget',
'created_at' => '2017-05-01'
],
(object) [
'name' => 'Tiddlywink',
'created_at' => '2018-03-01'
],
(object) [
'name' => 'Gizmo',
'created_at' => '2018-05-27'
]
];
答案 0 :(得分:4)
使用array_filter()
,并检查created_at
属性是否属于5月(第5个月)。为此,我们使用DateTime对象,并将format("m")
与5(m
进行比较,得出月份的数值为1到12)。
array_filter()
仅在返回值为true时保留元素。
$may_values = array_filter($data, function($obj) {
$date = new DateTime($obj->created_at);
return $date->format("m") == 5;
});
答案 1 :(得分:3)
这应该有效:
a = Split(StrConv(s, 64), Chr(0))
结果:
$new_array = [];
foreach($data as $v){
$date = date_parse($v->created_at);
if($date['month'] == 5){
$new_array[] = $v;
}
}
array(2) {
[0]=>
object(stdClass)#1 (2) {
["name"]=>
string(6) "Gadget"
["created_at"]=>
string(10) "2017-05-01"
}
[1]=>
object(stdClass)#3 (2) {
["name"]=>
string(5) "Gizmo"
["created_at"]=>
string(10) "2018-05-27"
}
}
的作用:(https://www.php.net/manual/en/function.date-parse.php)
date_parse()
演示:http://sandbox.onlinephpfunctions.com/code/8b639200d82661acdae7372f1762b02893b75f49
答案 2 :(得分:1)
我会说遍遍created_at值,然后选择月份---
$checkVal = date(m, strtotime($data['created_at']));
if($checkVal == 5)
...
5将是您想要的月份中的月份的#号。