php 获得上个月第一天和最后一天的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9735604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The best way to get the first and last day of last month?
提问by aaronroman
I'm looking for the best way to get the first and the last day of a last month. I use them for make SQL queries to get stats of last month.
我正在寻找获得上个月第一天和最后一天的最佳方式。我使用它们进行 SQL 查询以获取上个月的统计信息。
I think that this is the best way, more optimized but not more comprensive, anyone have another way to do the same?
我认为这是最好的方法,更优化但不是更全面,有人有另一种方法吗?
$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"))));
Thanks!!
谢谢!!
回答by Phen
In PHP 5.3, you can use the DateTime
class :
在 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
回答by user1064130
Last day of the previous month:
上个月的最后一天:
date("Y-m-d", mktime(0, 0, 0, date("m"), 0));
First day of the previous month:
上个月的第一天:
date("Y-m-d", mktime(0, 0, 0, date("m")-1, 1));
回答by Tarik
I use these in MySQL and PHP scripts, short and simple:
我在 MySQL 和 PHP 脚本中使用这些,简短而简单:
echo date('Y-m-d', strtotime('first day of last month'));
echo date('Y-m-d', strtotime('last day of last month'));
MySQL use example:
MySQL 使用示例:
$query = $db->query("SELECT * FROM mytable WHERE 1 AND mydate >= '".date('Y-m-d', strtotime('first day of last month'))."'");
回答by liquorvicar
If you're doing this for the purpose of a MySQL query, have you considered using the MONTH function, e.g.
如果您这样做是为了 MySQL 查询,您是否考虑过使用MONTH 函数,例如
SELECT [whatever stats you're after] FROM table
WHERE MONTH(date_field) = 12 and YEAR(date_field) = 2011
This would get your stats for December. If you start to experience performance problems and the historical data doesn't change, you might want to denormalise the data into an aggregate table (rolled up by the smallest increment you need, e.g. daily/hourly/monthly etc).
这将获得您 12 月的统计数据。如果您开始遇到性能问题并且历史数据没有改变,您可能希望将数据非规范化为一个聚合表(按您需要的最小增量汇总,例如每天/每小时/每月等)。
回答by Bazzz
you can do this in MySQL:
您可以在 MySQL 中执行此操作:
WHERE `DateAndTime` >= '2012-02-01 00:00:00'
AND `DateAndTime` < '2012-03-01 00:00:00'
回答by Ismael Miguel
let mysql deal with dates.
让 mysql 处理日期。
anything that is for the database to do, let it do.
数据库要做的任何事情,让它做。
like this:
像这样:
mysql_query("set @last_day=last_day(now()-interval 1 month),@first_day=(@last_day-interval 1 month)+interval 1 day");
$result=mysql_query("select * from `table` where (`date` between @first_day and @last_day)");
the best is that this will work even if the year changes.
最好的是,即使年份发生变化,这也能奏效。
just remember to change the database timezone to match php.
只需记住更改数据库时区以匹配 php。