php怎么获取数组里面时间是否为当天的时间?

array(5) {
[0]=>
array(2) {
["time"]=>
int(1671120000)
["lirun"]=>
int(72)
}
[1]=>
array(2) {
["time"]=>
int(1671033600)
["lirun"]=>
int(25)
}
[2]=>
array(2) {
["time"]=>
int(1671033600)
["lirun"]=>
int(25)
}
[3]=>
array(2) {
["time"]=>
int(1671033600)
["lirun"]=>
int(52)
}
[4]=>
array(2) {
["time"]=>
int(1670947200)
["lirun"]=>
int(30)
}
}

我要判断["time"]里面的时间戳是否当天的时间戳 然后所有["time"]是当天的数组里面的["lirun"]之和

//当天时间
$TodayStartTime = strtotime(date('Y-m-d',time())."00:00:00");
$TodayEndTime = strtotime(date('Y-m-d',time())."23:59:59");
//定义变量
$timesForToday = 0;
$sumForLirun = 0;
if(!empty($variable)){
foreach ($variable as $key => $value) {
if(!empty($value['time'])&&$value['time']>=$TodayStartTime&&$value['time']<=$TodayEndTime){
//判断计算条件
$timesForToday++;//计数
$sumForLirun = $sumForLirun+$value['lirun']; //求和
}
}
echo $timesForToday;
echo $sumForLirun;
exit;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-05-06
可以使用PHP内置函数 date() 和 time() 来判断数组中的时间是否为当天的时间
// 获取当前日期的时间戳
$today = strtotime(date('Y-m-d'));
foreach ($array as $item) {
// 判断时间戳是否为当天

if (strtotime(date('Y-m-d', $item['time'])) === $today) {
// 如果是当天,则执行相应操作
echo '是今天:' . $item['lirun'] . '<br>';
}
}
第2个回答  2023-01-13
循环选出大于今日凌晨的时间戳并且小于等于今日23:59:59的时间戳的就行了

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网