{"id":2193,"date":"2014-03-31T11:31:51","date_gmt":"2014-03-31T09:31:51","guid":{"rendered":"http:\/\/www.nyamsprod.com\/blog\/?p=2193"},"modified":"2018-04-27T10:53:58","modified_gmt":"2018-04-27T08:53:58","slug":"getting-the-first-day-of-the-previous-month-in-php","status":"publish","type":"post","link":"https:\/\/nyamsprod.com\/blog\/getting-the-first-day-of-the-previous-month-in-php\/","title":{"rendered":"Getting the first day of the previous month in PHP"},"content":{"rendered":"<div class=\"message warning\">\n<p><strong>Attention:<\/strong> Les informations de ce billet sont susceptibles d'&ecirc;tre obsol&egrave;tes car vieux de plus 2 ans.<\/p>\n<p><strong>Warning: <\/strong> The information you are reading may be obsolete, this post was published more than 2 years ago.<\/p>\n<\/div><p>Today a colleague of mine came to me with a simple question.<\/p>\n<blockquote><p>How to get the first day of the previous month in PHP ?<\/p><\/blockquote>\n<p>My colleague was using the following code:<\/p>\n<pre><code class=\"language-php\">&lt;?php\r\necho date('Y-m-01', strtotime('-1 MONTH')), PHP_EOL;\r\n<\/code><\/pre>\n<p>But the code would not work and would instead return the first day of the current month. <a title=\"beware when adding month in PHP\" href=\"http:\/\/be2.php.net\/manual\/en\/datetime.add.php#example-2322\" target=\"_blank\" rel=\"noopener\">This behavior is documented in the PHP documentation<\/a> so after some quick thinking here&#8217;s the code we ended up using:<\/p>\n<pre><code class=\"language-php\">&lt;?php\r\necho  date('Y-m-d', strtotime(date('Y-m-01').' -1 MONTH')), PHP_EOL;\r\n<\/code><\/pre>\n<p>We first detect the first day of the current month and then we substract 1 month.<\/p>\n<p>Now to be completely honest, this code sucks but we are still going to use it because it will run on any PHP version. But If you are using a version of PHP&gt;=5.3 you should know that the parsing algorithm has been improved. So next time you really need this information use the following code:<\/p>\n<pre><code class=\"language-php\">&lt;?php\r\ndate_default_timezone_set('UTC'); \/\/should be set in your script or in your php.ini\r\n$date = new DateTime('FIRST DAY OF PREVIOUS MONTH');\r\necho $date-&gt;format('Y-m-d'), PHP_EOL;\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using PHP to detect the first day of the previous month &#8230; the right way<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[770,771,412],"class_list":["post-2193","post","type-post","status-publish","format-standard","hentry","category-web","tag-date","tag-datetime","tag-php"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/comments?post=2193"}],"version-history":[{"count":5,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2193\/revisions"}],"predecessor-version":[{"id":2728,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2193\/revisions\/2728"}],"wp:attachment":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/media?parent=2193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/categories?post=2193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/tags?post=2193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}