{"id":252032,"date":"2015-04-24T11:00:05","date_gmt":"2015-04-24T11:00:05","guid":{"rendered":"https:\/\/osmosee.wordpress.com\/?p=565"},"modified":"2024-02-02T04:57:04","modified_gmt":"2024-02-02T04:57:04","slug":"using-1-month-with-strtotime-function-in-php","status":"publish","type":"post","link":"https:\/\/staging.osmosys.co\/uk\/using-1-month-with-strtotime-function-in-php\/","title":{"rendered":"Using &#8220;+\/-1 Month&#8221; with strtotime() function in PHP"},"content":{"rendered":"<div id=\"bsf_rt_marker\"><\/div><p>PHP&#8217;s date function strtotime is used to parse about any English textual date-time into a Unix time-stamp.<\/p>\n<p><strong>Syntax:<\/strong> int strtotime ( string $time [, int $now = time() ] );<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\necho strtotime(\"now\");\necho strtotime(\"+1 month\");\n?&gt;<\/pre>\n<p><strong><!--more-->PHP Docs<\/strong> : <a href=\"http:\/\/php.net\/manual\/en\/function.strtotime.php\" target=\"_blank\" rel=\"noopener\">http:\/\/php.net\/manual\/en\/function.strtotime.php<\/a><\/p>\n<p>We&#8217;ve been using it in one of our projects, to get the 28th day of the last month.<\/p>\n<p><strong>What we were expecting:<\/strong><\/p>\n<p>Assume that today&#8217;s date is<strong>: <\/strong>2014-12-31 and we wanted last month&#8217;s 28th.<\/p>\n<p>Our code &#8211;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php $date = date('Y-m-28',\u0393\u00f6\u00bc\u251c\u00ed strtotime(\u256c\u00f4\u251c\u00e7\u252c\u00fa-1 month\")) ?&gt;<\/pre>\n<p>What do you think the $date will have? 2014-11-28 right? Wrong! It has 2014-12-28<\/p>\n<p>Let&#8217;s look at another example &#8211;<\/p>\n<p>Assume that today&#8217;s date is 2015-01-31, and we want to get the next month&#8217;s 28th.<\/p>\n<p>Our code &#8211;<\/p>\n<pre>[code language=\"php\"]&amp;amp;lt;?php $date = &amp;amp;lt;strong&amp;amp;gt;date(&#039;Y-m-28&#039;, strtotime(+1 month&quot;))?&amp;amp;gt; [\/code]<\/pre>\n<p>The $date will have 2015-03-28, instead of 2015-02-28 as expected.<\/p>\n<p>Scratching your head? We were too, but delving a little deeper into the implementation made things clearer.<\/p>\n<p><strong>What&#8217;s happening?<\/strong><\/p>\n<p>If a month has 31 days and you are on the last day of that month, the strtotime(1 month&#8221;) returns the 1st day of that month.<\/p>\n<p>If you are on last day of January, the strtotime(+1 month&#8221;) returns 3rd of March(not a leap year) \/ 2nd March(leap year).<\/p>\n<p><strong>Why it&#8217;s not a bug:<\/strong><\/p>\n<p>The current behavior is correct (in it&#8217;s own way). Let&#8217;s understand the process with an example:<\/p>\n<ul>\n<li>Assume today&#8217;s date is 2015-01-31, and you want to add&#8221;+1 month&#8221; to it.<\/li>\n<li>Actually +1 month increases the month number (originally 1) by one. This makes the date 2015-02-31.<\/li>\n<li>But the 2nd month (February) only has 28 days in 2015, so PHP auto-corrects this by just continuing to count days from February 1st. You then end up at March 3rd.You can see this here: <a href=\"http:\/\/ideone.com\/HNalGr\" target=\"_blank\" rel=\"noopener\">http:\/\/ideone.com\/HNalGr<\/a><\/li>\n<li>A similar thing happens, when you run&#8221;-1 month&#8221;.\u0393\u00f6\u00bc\u251c\u00edYou can see this here: <a href=\"http:\/\/Ideone.Com\/Yvm5p1\" target=\"_blank\" rel=\"noopener\">http:\/\/Ideone.Com\/Yvm5p1<\/a><\/li>\n<\/ul>\n<p><strong>How to get what you want:<\/strong><\/p>\n<p>If you are using PHP 5.3 and above, to obtain the desired behavior, you can use the relative time stanza &#8211; <strong>first day of<\/strong>. This stanza can be used in combination with next month, fifth month or +9 months to go to the first day of the specified month. Instead of +1 month from what you&#8217;re doing, you can use this code to get the first day of next month like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$d = new DateTime('2015-01-31');\n$d-&gt;modify('first day of next month');\necho $d-&gt;format('Y-m-28');\n?&gt;<\/pre>\n<p><strong>How it works(the above solution):<\/strong><\/p>\n<ol>\n<li>The <strong>new DateTime<\/strong> creates a date object of the date that we given as parameter or today date.<\/li>\n<li>Now, we are modifying the date to&#8221;first day of next month&#8221; (or&#8221;first day of last month&#8221;).<\/li>\n<li>Then we are echoing the date in the format that we want (i.e., yyyy-mm-28) by using format functionality of date<\/li>\n<\/ol>\n<p><strong>You can see this example(with execution) here:<\/strong><\/p>\n<ol>\n<li><a href=\"http:\/\/ideone.com\/jw25JV\" target=\"_blank\" rel=\"noopener\">http:\/\/ideone.com\/jw25JV<\/a><\/li>\n<\/ol>\n<ol start=\"2\">\n<li><a href=\"http:\/\/ideone.com\/pStgEK\" target=\"_blank\" rel=\"noopener\">http:\/\/ideone.com\/pStgEK<\/a><\/li>\n<\/ol>\n<p><strong>A universal solution to get what you want:<\/strong><\/p>\n<p>If you are using PHP 5.3 or above, we recommend going with the above solution. Otherwise, you can use PHP&#8217;s mktime function to get what you need.<\/p>\n<p><strong>PHP Docs<\/strong> : <a href=\"http:\/\/php.net\/manual\/en\/function.mktime.php\" target=\"_blank\" rel=\"noopener\">http:\/\/php.net\/manual\/en\/function.mktime.php<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$months = n; \/\/ Here n = ..-2,-1,0,1,2, ..(months to add or subtract)\n$years = n; \/\/ Here n = ..-2,-1,0,1,2,..(years to add or subtract)\necho date('Y-m-28', mktime(0, 0, 0, date('m')+$months, 1, date('Y') + $years));\n?&gt;<\/pre>\n<p><strong>You can see this example(with execution) here:<\/strong><\/p>\n<p><a href=\"http:\/\/ideone.com\/tgP1OT\" target=\"_blank\" rel=\"noopener\">http:\/\/ideone.com\/tgP1OT<\/a><\/p>\n<p>The following are some more examples to understand better that how to use strtotime.<\/p>\n<p>PHP fiddle link: <a href=\"http:\/\/ideone.com\/VRMCyW\" target=\"_blank\" rel=\"noopener\">http:\/\/ideone.com\/VRMCyW<br \/>\n<\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo Date(\"Y-m-d\", strtotime(\"2013-01-01 +1 Month -1 Day\"));\/\/ 2013-01-31\n\necho Date(\"Y-m-d\", strtotime(\"2013-01-31 +1 Month -3 Day\"));\/\/ 2013-02-28\n\necho Date(\"Y-m-d\", strtotime(\"2013-01-31 +2 Month\"));\/\/ 2013-03-31\n\necho Date(\"Y-m-d\", strtotime(\"2013-01-31 +3 Month -1 Day\"));\/\/ 2013-04-30\n\necho Date(\"Y-m-d\", strtotime(\"2013-12-31 -1 Month -1 Day\"));\/\/ 2013-11-30\n\necho Date(\"Y-m-d\", strtotime(\"2013-12-31 -2 Month\"));\/\/ 2013-10-31\n\necho Date(\"Y-m-d\", strtotime(\"2013-12-31 -3 Month\"));\/\/ 2013-10-01\n\necho Date(\"Y-m-d\", strtotime(\"2013-12-31 -3 Month -1 Day\"));\/\/ 2013-09-30<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP&#8217;s date function strtotime is used to parse about any English textual date-time into a Unix time-stamp. Syntax: int strtotime ( string $time [, int $now = time() ] ); Examples: &lt;?php echo strtotime(&#8220;now&#8221;); echo strtotime(&#8220;+1 month&#8221;); ?&gt;<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[39],"tags":[],"class_list":["post-252032","post","type-post","status-publish","format-standard","hentry","category-general"],"modified_by":null,"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/posts\/252032","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/comments?post=252032"}],"version-history":[{"count":0,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/posts\/252032\/revisions"}],"wp:attachment":[{"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/media?parent=252032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/categories?post=252032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.osmosys.co\/uk\/wp-json\/wp\/v2\/tags?post=252032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}