如何在php中获得最后x (x = 3,6或12)个月的第一天和最后一天


How to get the first and last day of the last x (x = 3, 6 or 12) months in php?

如何获得最后x个月的第一天和最后一天(我将用3,6和12替换x) ?我知道最后一个月将是:date("Y-n-j", strtotime("first day of previous month"));

可以使用DateTime对象:

$myDate = new 'DateTime("last day of next month");
$format = $myDate->format("Y-n-d");

返回:2015-11-30

您可以在这里找到Relative Formats: http://php.net/manual/en/datetime.formats.relative.php

希望对你有帮助

你已经知道一个月从1号开始。t指定符为date提供给定月份中的天数。您可以将给定月份的这两个值组合起来,以确定第一天和最后一天。在此基础上,你只需要加减3、6和12个月,但这个练习取决于你。

<?php
$first_of_this_month = date("Y-m-01");
$last_of_this_month = date("Y-m-t")