【问题标题】:Calculate Orders on a weekly and monthly basis in php在php中每周和每月计算订单
【发布时间】:2012-05-30 17:08:21
【问题描述】:

我需要每周和每月计算订单。到目前为止我设置的代码是这样的:

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'];
}

这里的问题是,如果日期是星期一,那么它会是上星期一和下个星期日。我需要它来计算那个星期一,而不是下一个。这个月也一样。总而言之,我只是在寻找最准确和最简单的答案。它不必是格式。每月和每周。

【问题讨论】:

  • 您查看过strtotime 手册页上的示例吗?
  • 查看date 函数提供的不同格式参数。然后请解释为什么它们都不合适。如果有一个合适的(正如您可能猜到的,有一个;)),也可以命名。提前恭喜!

标签: php cakephp-1.3


【解决方案1】:

使用现有代码,您可以在定义密钥之前添加一个 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));
}

您可能需要检查“今天”是否是当天的实际文本表示。

【讨论】:

    【解决方案2】:

    如果你使用mysql,你可以让sql做很多工作。

    $x = mysql_query("SELECT * FROM `mysales` 
                 WHERE `MerchantID`=’$id’
                 AND `Saledate`  > ’TIMESTAMP(".strtotime(’-1 week’).")’");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多