计算活动天数表数据库 [英] count number of active days form database
本文介绍了计算活动天数表数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我在MySQL数据库中有此数据.
Hi everyone i have this data in MySQL database.
Date | status
---------------------
2017-03-01 | active
2014-03-10 | suspend
2014-03-20 | active
2014-03-25 | suspend
我要做的是计数并显示从给定日期开始,该状态为活动状态的天数.这是我到目前为止尝试过的
what i want to do is count and display number of days from the given date that the status was on active state. here is what i tried so far
foreach ($get_status as $status) {
if ($status['status'] == 'suspended') {
$date1 = date_create($status['date']);
$date2 = date_create("2017-03-01");
$diff = date_diff($date2, $date1);
$active_days += $diff->format("%a days");
echo $active_days;
}
}
我希望输出为+14天,但是此代码/算法没有给我正确的计算,而且我不知道该怎么办.请帮助一个兄弟!
i am expecting the output to be +14 days , but this code/algorithm is not giving me the right calculation and i don't know what to do.please help a brother out!
推荐答案
我遇到了您的问题,您可以通过替换
I got your issue you can fix this by replacing
$active_days += $diff->format("%a days");
使用
$active_days += $diff->days;
它将正常工作.
这篇关于计算活动天数表数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文