当前位置: 首页 > news >正文

【PHP】简记问题:使用strtotime(‘-1 month‘, time)获取上个月第一天时间戳出错

发生场景

在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}"

问题排查

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 - Manual

strtotime("-1 month",time());

以当前时间戳的日数据在上个月找到对应的日数据,即(3月01日找2月01日,因此3月29号在2月份找不到29号,直接往后+1天,即3月01号

解决(官网) 

# first day of last month 上个月的最后一天
# strtotime(date('Ymd')) 当天的时间戳
date('Ymd',strtotime("first day of last month", strtotime(date('Ymd'))))

public function get_order_lastmomth_money(){$day = strtotime("January 1, 2023");for ($i = 0;$i < 365;$i++) {echo "当前日期:".date("Ymd",$day + ($i * 86400)) . "-上月月初日期:" . date("Ymd", strtotime("first day of last month", $day + ($i * 86400))) . "<br />";}
}

 感谢阅读,欢迎讨论

http://www.lryc.cn/news/103323.html

相关文章:

  • 舌体分割的初步展示应用——依托Streamlit搭建demo
  • 从Vue层面 - 解析发布订阅模式和观察者模式区别
  • 面向对象之_多态_1
  • Spring学习笔记之spring概述
  • 旧项目导入Eclipse时文件夹看起来乱七八糟,无从下手的解决办法(无main或webapp等文件夹)
  • Reinforcement Learning with Code 【Code 2. Tabular Sarsa】
  • 服务调用---------Ribbon和Feign
  • app自动化测试之Appium问题分析及定位
  • 婚庆服务小程序app开发方案详解
  • 集合简述
  • 常见的软件测试面试题汇总
  • 学习笔记|大模型优质Prompt开发与应用课(二)|第二节:超高产文本生成机,传媒营销人必备神器
  • Linux基础-4
  • oracle-创建函数
  • 【Ansible 的脚本 --- playbook 剧本】
  • ubuntu释放缓存
  • 实用调试技巧(1)
  • uniapp:H5定位当前省市区街道信息
  • 自然语言处理从入门到应用——LangChain:提示(Prompts)-[提示模板:部分填充的提示模板和提示合成]
  • 论文笔记--GloVe: Global Vectors for Word Representation
  • day57|● 647. 回文子串 ● 516.最长回文子序列
  • docker compose.yml学习
  • 【业务功能篇55】Springboot+easyPOI 导入导出
  • 对顶堆算法
  • node.js的优点
  • golang编译跨平台
  • 关于Spring的bean的相关注解以及其简单使用方法
  • 【计算机视觉】BLIP:源代码示例demo(含源代码)
  • TWILIGHT靶场详解
  • 【案例】--GPT衍生应用案例