本文介紹了獲得上個月第一天和最后一天的最佳方法是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在尋找獲取上個月第一天和最后一天的最佳方式.我使用它們進行 SQL 查詢以獲取上個月的統計信息.
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.
我認為這是最好的方法,更優化但不是更全面,有人有其他方法可以做到嗎?
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"))));
謝謝!!
推薦答案
在 PHP 5.3 中,您可以使用 DateTime
類:
In PHP 5.3, you can use the DateTime
class :
<?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
這篇關于獲得上個月第一天和最后一天的最佳方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!