{"id":195,"date":"2009-11-23T09:11:40","date_gmt":"2009-11-23T09:11:40","guid":{"rendered":"http:\/\/www.sandykadam.com\/?p=195"},"modified":"2012-01-03T12:09:56","modified_gmt":"2012-01-03T12:09:56","slug":"find-start-and-end-date-of-week-in-php","status":"publish","type":"post","link":"https:\/\/www.sandykadam.com\/blog\/programming-stuff\/php\/find-start-and-end-date-of-week-in-php\/","title":{"rendered":"Find start and end date of week in PHP"},"content":{"rendered":"<p>I need to check if the current day is a Monday and then figure out the dates of last week\u2019s Monday and Sunday. Here is my solution which is simpler:<\/p>\n<p>[code type=php]<br \/>\n<!--?php <br ?--> if(date(&#8216;N&#8217;) == 1) {<br \/>\n$prior_week = date(&#8216;W&#8217;) &#8211; 1;<br \/>\nif($prior_week == 0) {<br \/>\n$prior_week = 52;<br \/>\n$year = date(&#8216;Y&#8217;) &#8211; 1;<br \/>\n}else<br \/>\n$year = date(&#8216;Y&#8217;);<\/p>\n<p>echo date(&#8220;Y-m-d&#8221;, strtotime($year.&#8217;W&#8217;.$prior_week.&#8217;1&#8242;));<br \/>\necho date(&#8220;Y-m-d&#8221;, strtotime($year.&#8217;W&#8217;.$prior_week.&#8217;7&#8242;));<br \/>\n}<\/p>\n<p>?&gt;<br \/>\n[\/code]<\/p>\n<p>So if we have a Monday which we have when date(&#8216;N&#8217;) is 1, we get the current week number of the year with date(&#8216;W&#8217;). Last week&#8217;s number should then be that number minus one. Note that we need to take extra precautions to to switch the year to the prior year if we have to.<\/p>\n<p>Finally we use the old date and strtotime combo to get the dates in any format we want, in this case as YYYY-MM-DD. The format used in the strtotime call is an ISO8601 format that incorporates the day of week number and the week of year number. Without strtotime&#8217;s ability to understand this format the above solution would not work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I need to check if the current day is a &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"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":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[24],"tags":[37],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-php","tag-php-2"],"aioseo_notices":[],"views":703,"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p25uYH-39","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":4,"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":197,"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions\/197"}],"wp:attachment":[{"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sandykadam.com\/blog\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}