我有一个要填充的条形图,需要 7 和 1 为一周中的每一天,周一 - 周日。
星期一是工作周的开始,星期日是一周的最后一天。
数据库中的数据如下所示。
user1|2013-04-13 16:53:55|1
user1|2013-03-13 16:53:55|1
user1|2013-03-13 15:53:55|1
user1|2013-02-13 09:53:55|1
user1|2013-04-13 07:53:55|1
user2|2013-03-13 15:53:55|1
行数是每个用户的总和
这是一种找出星期几的方法。
//find day of week
$dayofweek = date('l', strtotime(date("Y-m-d H:i:s")));
$dates = array();
if ($dayofweek == "Monday") {
$dates['monday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Monday last week' ));
$dates['monday']['evening']= date( 'Y-m-d 23:59:59', strtotime( 'Monday last week' ));
$dates['tuesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Tuesday this week' ));
$dates['tuesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Tuesday this week' ));
$dates['wednesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Wednesday this week' ));
$dates['wednesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Wednesday this week' ));
$dates['thursday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Thursday this week' ));
$dates['thursday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Thursday this week' ));
$dates['friday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Friday this week' ));
$dates['friday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Friday this week' ));
$dates['saturday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Saturday this week' ));
$dates['saturday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Saturday this week' ));
$dates['sunday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Sunday this week' ));
$dates['sunday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Sunday this week' ));
}
elseif ($dayofweek == "Tuesday") {
$dates['monday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Monday last week' ));
$dates['monday']['evening']= date( 'Y-m-d 23:59:59', strtotime( 'Monday last week' ));
$dates['tuesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Tuesday last week' ));
$dates['tuesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Tuesday last week' ));
$dates['wednesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Wednesday this week' ));
$dates['wednesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Wednesday this week' ));
$dates['thursday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Thursday this week' ));
$dates['thursday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Thursday this week' ));
$dates['friday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Friday this week' ));
$dates['friday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Friday this week' ));
$dates['saturday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Saturday this week' ));
$dates['saturday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Saturday this week' ));
$dates['sunday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Sunday this week' ));
$dates['sunday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Sunday this week' ));
}
elseif ($dayofweek == "Wednesday") {
$dates['monday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Monday last week' ));
$dates['monday']['evening']= date( 'Y-m-d 23:59:59', strtotime( 'Monday last week' ));
$dates['tuesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Tuesday last week' ));
$dates['tuesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Tuesday last week' ));
$dates['wednesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Wednesday last week' ));
$dates['wednesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Wednesday last week' ));
$dates['thursday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Thursday this week' ));
$dates['thursday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Thursday this week' ));
$dates['friday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Friday this week' ));
$dates['friday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Friday this week' ));
$dates['saturday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Saturday this week' ));
$dates['saturday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Saturday this week' ));
$dates['sunday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Sunday this week' ));
$dates['sunday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Sunday this week' ));
}
elseif ($dayofweek == "Thursday") {
$dates['monday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Monday last week' ));
$dates['monday']['evening']= date( 'Y-m-d 23:59:59', strtotime( 'Monday last week' ));
$dates['tuesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Tuesday last week' ));
$dates['tuesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Tuesday last week' ));
$dates['wednesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Wednesday last week' ));
$dates['wednesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Wednesday last week' ));
$dates['thursday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Thursday last week' ));
$dates['thursday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Thursday last week' ));
$dates['friday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Friday this week' ));
$dates['friday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Friday this week' ));
$dates['saturday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Saturday this week' ));
$dates['saturday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Saturday this week' ));
$dates['sunday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Sunday this week' ));
$dates['sunday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Sunday this week' ));
}
elseif ($dayofweek == "Friday") {
$dates['monday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Monday last week' ));
$dates['monday']['evening']= date( 'Y-m-d 23:59:59', strtotime( 'Monday last week' ));
$dates['tuesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Tuesday last week' ));
$dates['tuesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Tuesday last week' ));
$dates['wednesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Wednesday last week' ));
$dates['wednesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Wednesday last week' ));
$dates['thursday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Thursday last week' ));
$dates['thursday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Thursday last week' ));
$dates['friday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Friday last week' ));
$dates['friday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Friday last week' ));
$dates['saturday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Saturday this week' ));
$dates['saturday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Saturday this week' ));
$dates['sunday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Sunday this week' ));
$dates['sunday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Sunday this week' ));
}
elseif ($dayofweek == "Saturday") {
$dates['monday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Monday last week' ));
$dates['monday']['evening']= date( 'Y-m-d 23:59:59', strtotime( 'Monday last week' ));
$dates['tuesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Tuesday last week' ));
$dates['tuesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Tuesday last week' ));
$dates['wednesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Wednesday last week' ));
$dates['wednesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Wednesday last week' ));
$dates['thursday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Thursday last week' ));
$dates['thursday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Thursday last week' ));
$dates['friday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Friday last week' ));
$dates['friday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Friday last week' ));
$dates['saturday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Saturday last week' ));
$dates['saturday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Saturday last week' ));
$dates['sunday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Sunday this week' ));
$dates['sunday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Sunday this week' ));
}
elseif ($dayofweek == "Sunday") {
$dates['monday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Monday last week' ));
$dates['monday']['evening']= date( 'Y-m-d 23:59:59', strtotime( 'Monday last week' ));
$dates['tuesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Tuesday last week' ));
$dates['tuesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Tuesday last week' ));
$dates['wednesday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Wednesday last week' ));
$dates['wednesday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Wednesday last week' ));
$dates['thursday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Thursday last week' ));
$dates['thursday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Thursday last week' ));
$dates['friday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Friday last week' ));
$dates['friday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Friday last week' ));
$dates['saturday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Saturday last week' ));
$dates['saturday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Saturday last week' ));
$dates['sunday']['morning'] = date( 'Y-m-d 00:00:01', strtotime( 'Sunday last week' ));
$dates['sunday']['evening'] = date( 'Y-m-d 23:59:59', strtotime( 'Sunday last week' ));
}
我应该为一周中的每一天创建七个查询 1 吗?
SELECT myDateTime FROM `mylog` WHERE myDateTime >= $dates['monday']['morning'] AND myDateTime <= $dates['monday']['evening']
还是有更简单更有效的方法?
谢谢。
=========================编辑1======================== ====
这是我到目前为止所拥有的。
SELECT date( myDateTime ) , sum( myValue )
FROM mylog
GROUP BY date( myDateTime )
如何像星期一一样指定我想要的日期?
有没有办法把它变成 1 个查询而不是 7 个?
谢谢。