我需要计算每周和每月的订单。到目前为止,我设置的代码如下所示:
foreach ($commissions as $c) {
$ts = strtotime($c['order_date']);
$lastWeekKey = date("Y-m-d", strtotime('last monday', $ts));
$nextWeekKey = date("Y-m-d", strtotime('next sunday', $ts));
$stringDays = $lastWeekKey." to ".$nextWeekKey;
if (!isset($dates[$stringDays])) {
$dates[$stringDays] = 0;
}
$dates[$stringDays] += $c['projected_payment'];
}这里的问题是,如果日期落在星期一,那么就是上个星期一和下个星期天。我要它在那个星期一算,而不是下个。这个月的情况也是如此。总而言之,我只是在寻找最准确和最简单的答案。它不一定要采用以下格式。包括月度和周度。
发布于 2012-05-31 05:16:07
使用现有代码,您只需在定义键之前添加一个if语句即可:
$thisday=getdate($ts);
if($thisday==1) { //1 = Monday
$lastWeekKey = date("Y-m-d", strtotime('today',$ts));
$nextWeekKey = date("Y-m-d", strtotime('next sunday',$ts));
} else {
$lastWeekKey = date("Y-m-d", strtotime('next monday',$ts));
$nextWeekKey = date("Y-m-d", strtotime('next sunday',$ts));
}您可能需要检查“今天”是否为当天的实际文本表示。
发布于 2012-05-31 05:42:15
如果你使用mysql,你可以让sql做很多工作。
$x = mysql_query("SELECT * FROM `mysales`
WHERE `MerchantID`=’$id’
AND `Saledate` > ’TIMESTAMP(".strtotime(’-1 week’).")’");https://stackoverflow.com/questions/10820969
复制相似问题