<p>I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?</p>

[{'answer_id': 43607, 'author': 'Biri', 'author_id': 968, 'author_profile': 'https://Stackoverflow.com/users/968', 'pm_score': 3, 'selected': False, 'text': '<p>First day is always YYYY-MM-01, isn\'t it? Example: <code>date("Y-M-d", mktime(0, 0, 0, 8, 1, 2008))</code></p>\n\n<p>Last day is the previous day of the next month\'s first day:</p>\n\n<pre><code>$date = new DateTime("2008-09-01");\n$date-&gt;modify("-1 day");\necho $date-&gt;format("Y-m-d");\n</code></pre>\n'}, {'answer_id': 43609, 'author': 'Niyaz', 'author_id': 184, 'author_profile': 'https://Stackoverflow.com/users/184', 'pm_score': 2, 'selected': False, 'text': '<p>The first day of the month is always 1.\nSo it will become</p>\n\n<pre><code>YYYY-MM-01\n</code></pre>\n\n<p>the last day can be calculated as:</p>\n\n<pre><code>&lt;?php\n    $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31\n    echo "There was $num days in August 2003";\n?&gt;\n</code></pre>\n'}, {'answer_id': 43610, 'author': 'Michał Niedźwiedzki', 'author_id': 2169, 'author_profile': 'https://Stackoverflow.com/users/2169', 'pm_score': 7, 'selected': True, 'text': '<pre><code>$first = date(\'Y-m-d\', mktime(0, 0, 0, $month, 1, $year));\n$last = date(\'Y-m-t\', mktime(0, 0, 0, $month, 1, $year));\n</code></pre>\n\n<p>See <a href="http://ie2.php.net/date" rel="noreferrer">date()</a> in PHP documentation.</p>\n'}, {'answer_id': 43618, 'author': 'ZombieSheep', 'author_id': 377, 'author_profile': 'https://Stackoverflow.com/users/377', 'pm_score': 2, 'selected': False, 'text': "<p>OK, first is dead easy.  </p>\n\n<pre><code>date ('Y-m-d', mktime(0,0,0,MM,01,YYYY));\n</code></pre>\n\n<p>Last is a little trickier, but not much. </p>\n\n<pre><code>date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY));\n</code></pre>\n\n<p>If I remember my PHP date stuff correctly...</p>\n\n<p>**edit - Gah!  Beaten to it about a million times...</p>\n\n<p>Edit by Pat:</p>\n\n<p>Last day should have been </p>\n\n<pre><code>date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1\n</code></pre>\n"}, {'answer_id': 43634, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 0, 'selected': False, 'text': "<p>By the way @ZombieSheep solution </p>\n\n<pre><code>date ('Y-m-d', mktime(0,0,0,$MM + 1,-1,$YYYY));\n</code></pre>\n\n<p>does not work it should be </p>\n\n<pre><code>date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1\n</code></pre>\n\n<p>Of course @Michał Słaby's accepted solution is the simplest.</p>\n"}, {'answer_id': 50065, 'author': 'Thomas Owens', 'author_id': 572, 'author_profile': 'https://Stackoverflow.com/users/572', 'pm_score': 0, 'selected': False, 'text': '<p>Just to verify that I didn\'t miss any loose ends:</p>\n\n<pre><code>$startDay = 1;\n\nif (date("m") == 1) {\n    $startMonth = 12;\n    $startYear = date("Y") - 1;\n\n    $endMonth = 12;\n    $endYear = date("Y") - 1;\n}\nelse {\n    $startMonth = date("m") - 1;\n    $startYear = date("Y");\n\n    $endMonth = date("m") - 1;\n    $endYear = date("Y");\n}\n\n$endDay = date("d") - 1;\n\n$startDate = date(\'Y-m-d\', mktime(0, 0, 0, $startMonth , $startDay, $startYear));\n$endDate = date(\'Y-m-d\', mktime(0, 0, 0, $endMonth, $endDay, $endYear));\n</code></pre>\n'}, {'answer_id': 9225401, 'author': 'Eranda', 'author_id': 1181127, 'author_profile': 'https://Stackoverflow.com/users/1181127', 'pm_score': 2, 'selected': False, 'text': '<pre><code>&lt;?php\necho "Month Start - " . $monthStart = date("Y-m-1") . "&lt;br/&gt;";\n$num = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));\necho "Monthe End - " . $monthEnd = date("Y-m-".$num);\n?&gt;\n</code></pre>\n'}, {'answer_id': 16603663, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>The easiest way to do this with PHP is </p>\n\n<pre><code>$dateBegin = strtotime("first day of last month");  \n$dateEnd = strtotime("last day of last month");\n\necho date("MYDATEFORMAT", $dateBegin);  \necho "&lt;br&gt;";        \necho date("MYDATEFORMAT", $dateEnd);\n</code></pre>\n\n<p>Or the last week</p>\n\n<pre><code>if (date(\'N\', time()) == 7) {\n$dateBegin = strtotime("-2 weeks Monday");\n$dateEnd = strtotime("last Sunday");\n} else {\n$dateBegin = strtotime("Monday last week"); \n$dateEnd = strtotime("Sunday last week");   \n}\n</code></pre>\n\n<p>Or the last year</p>\n\n<pre><code>$dateBegin = strtotime("1/1 last year");\n$dateEnd = strtotime("12/31 this year");\n</code></pre>\n'}, {'answer_id': 17627103, 'author': 'Nate', 'author_id': 1205600, 'author_profile': 'https://Stackoverflow.com/users/1205600', 'pm_score': 0, 'selected': False, 'text': "<p>try this to get the number of days in the month:</p>\n\n<pre><code>$numdays = date('t', mktime(0, 0, 0, $m, 1, $Y));\n</code></pre>\n"}, {'answer_id': 27856580, 'author': 'csonuryilmaz', 'author_id': 1750142, 'author_profile': 'https://Stackoverflow.com/users/1750142', 'pm_score': 0, 'selected': False, 'text': "<p>Example; I want to get first day and last day of current month.</p>\n\n<pre><code>$month   = (int) date('F');\n$year    = (int) date('Y');\n\ndate('Y-m-d', mktime(0, 0, 0, $month + 1, 1, $year)); //first\ndate('Y-m-d', mktime(0, 0, 0, $month + 2, 0, $year)); //last\n</code></pre>\n\n<p>When you run this for instance at date 2015-01-09, the first and last values will be sequentially;</p>\n\n<pre><code>2015-01-01\n2015-01-31\n</code></pre>\n\n<p>Tested. </p>\n"}, {'answer_id': 48094516, 'author': 'arturwwl', 'author_id': 8367985, 'author_profile': 'https://Stackoverflow.com/users/8367985', 'pm_score': 0, 'selected': False, 'text': '<p>From <a href="https://stackoverflow.com/questions/39438496/last-day-of-next-month-date-timestamp-in-php">here(get next month last day)</a> that is marked as duplicated, so i can\'t add comment there, but people can got bad answers from there. </p>\n\n<p>Correct one for last day of next month:</p>\n\n<pre><code>echo ((new DateTime(date(\'Y-m\').\'-01\'))-&gt;modify(\'+1 month\')-&gt;format(\'Y-m-t\'));\n</code></pre>\n\n<p>Correct one for first day of next month:</p>\n\n<pre><code>echo ((new DateTime(date(\'Y-m\').\'-01\'))-&gt;modify(\'+1 month\')-&gt;format(\'Y-m-01\'));\n</code></pre>\n\n<p>Code like this will be providing March from January, so that\'s not what could be expected.</p>\n\n<p><code>echo ((new DateTime())-&gt;modify(\'+1 month\')-&gt;format(\'Y-m-t\'));</code></p>\n'}, {'answer_id': 70531006, 'author': 'palmer', 'author_id': 1390834, 'author_profile': 'https://Stackoverflow.com/users/1390834', 'pm_score': 0, 'selected': False, 'text': '<p>proper way to build a relative date from now is:</p>\n<pre><code>//bad example - will be broken when generated at 30 of December (broken February)\n    echo date(&quot;Y-m-d&quot;, strtotime(&quot;now&quot;)).&quot;\\n&quot;;\n    echo date(&quot;Y-m-d&quot;, strtotime(&quot;now + 1 month&quot;)).&quot;\\n&quot;;\n    echo date(&quot;Y-m-d&quot;, strtotime(&quot;now + 2 month&quot;)).&quot;\\n&quot;;\n    echo date(&quot;Y-m-d&quot;, strtotime(&quot;now + 3 month&quot;)).&quot;\\n&quot;;\n    \n//good example, you can change first day to last day or any day\n    echo date(&quot;Y-m-d&quot;, strtotime(&quot;first day of this month&quot;)).&quot;\\n&quot;;\n    echo date(&quot;Y-m-d&quot;, strtotime(&quot;first day of next month&quot;)).&quot;\\n&quot;;\n    echo date(&quot;Y-m-d&quot;, strtotime(&quot;first day of +2 month&quot;)).&quot;\\n&quot;;\n    echo date(&quot;Y-m-d&quot;, strtotime(&quot;first day of +3 month&quot;)).&quot;\\n&quot;;\n</code></pre>\n<p>and the result will be:</p>\n<pre><code>2021-12-30\n2022-01-30\n2022-03-02\n2022-03-30\n\n2021-12-01\n2022-01-01\n2022-02-01\n2022-03-01\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/43589', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/572/']