发生场景
在7月31号是查看统计上个月订单购买总金额,查询结果为0
$preMonthStart = strtotime(date('Ym01', strtotime("-1 month")));
$curMonthStart = strtotime(date('Ym01'));
# 统计上月份实际订单金额
$sql = "SELECT count(money) FROM orders WHERE confirm_time > {$preMonthStart} AND confirm_time < {$curMonthStart}"
问题排查
操作说明:读取2023年整年每一天对应的上月月初的日期,比对日期
public function get_order_lastmomth_money()
{
$day = strtotime("January 1, 2023");
for ($i = 0;$i < 365;$i++) {
echo "当前日期:".date("Ymd",$day + ($i * 86400)) . "-上月月初日期:" . date("Ym01", strtotime("-1 Month", $day + ($i * 86400))) . "<br />";
}
}



文章讲述了在使用PHP的strtotime函数查询上个月订单总金额时遇到的问题,由于-1month会根据当前日期调整导致计算错误。官方文档指出了解决方案,即使用firstdayoflastmonth来获取上个月的第一天,从而正确计算上月订单金额。提供了一个修正后的代码示例。
最低0.47元/天 解锁文章
908

被折叠的 条评论
为什么被折叠?



