aar*_*man 43 php mysql optimization date
我正在寻找获得上个月第一天和最后一天的最佳方式.我使用它们进行SQL查询以获取上个月的统计数据.
我认为这是最好的方式,更优化但不是更加全面,任何人都有另一种方式来做同样的事情?
$month_ini = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime("-1 month")), 1, date("Y", strtotime("-1 month"))));
$month_end = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime("-1 month")), date("t", strtotime("-1 month")), date("Y", strtotime("-1 month"))));
Run Code Online (Sandbox Code Playgroud)
谢谢!!
Phe*_*hen 120
在PHP 5.3中,您可以使用以下DateTime类:
<?php
$month_ini = new DateTime("first day of last month");
$month_end = new DateTime("last day of last month");
echo $month_ini->format('Y-m-d'); // 2012-02-01
echo $month_end->format('Y-m-d'); // 2012-02-29
Run Code Online (Sandbox Code Playgroud)
小智 59
上个月的最后一天:
date("Y-m-d", mktime(0, 0, 0, date("m"), 0));
Run Code Online (Sandbox Code Playgroud)
上个月的第一天:
date("Y-m-d", mktime(0, 0, 0, date("m")-1, 1));
Run Code Online (Sandbox Code Playgroud)
我在MySQL和PHP脚本中使用这些,简短而简单:
echo date('Y-m-d', strtotime('first day of last month'));
echo date('Y-m-d', strtotime('last day of last month'));
Run Code Online (Sandbox Code Playgroud)
MySQL使用示例:
$query = $db->query("SELECT * FROM mytable WHERE 1 AND mydate >= '".date('Y-m-d', strtotime('first day of last month'))."'");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47595 次 |
| 最近记录: |