{"id":12125,"date":"2010-02-10T23:11:57","date_gmt":"2010-02-10T21:11:57","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=12125"},"modified":"2010-02-12T00:16:52","modified_gmt":"2010-02-11T22:16:52","slug":"php-date-and-53-weeks","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2010\/02\/10\/php-date-and-53-weeks\/","title":{"rendered":"PHP date() and 53 weeks"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>Let&#8217;s say you have a bunch of statistical data. \u00a0And all that data is date-related. \u00a0And let&#8217;s say want to display that data on a chart, a weekly average or something along those lines. \u00a0One of the ways for you to place the value into the proper week would be something like this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$week = date('W', strtotime($stats_date));\r\n$values&#x5B;$week]&#x5B;] = $stats_value;\r\n<\/pre>\n<p>And if you did it this way, sooner or later, you&#8217;d notice that something is not quite working right at the edges of your chart. \u00a0With code as simple and straight-forward as this, you&#8217;d probably look for the problem elsewhere. \u00a0Maybe it&#8217;s your statistical data which is wrong, or the graph is not generated properly. \u00a0But the problem is here.<\/p>\n<p>How many weeks do you think there are in a year? \u00a0A common knowledge says 52. \u00a0However, if you think for a moment about how the weeks are related to the year, you&#8217;ll realize that the first and last weeks don&#8217;t necessary start and end at the edge of the year. \u00a0If you play around with 1st of January and 31st of December across several years, you&#8217;ll notice that sometimes they fall into the 53rd week. \u00a0(As do a few more days, not just these two).<\/p>\n<p>And here, the problem with the &#8220;W&#8221; date() format starts to emerge. \u00a0 When scattering your data across a single year, you&#8217;d most often expect January to start with the first week of the year. \u00a0But it doesn&#8217;t. Sometimes the start of it falls into the 53rd week of the previous year. \u00a0And <em>date(&#8216;W&#8217;, $your_time)<\/em> will happily return 53. \u00a0What will this do to your chart? \u00a0Two things are most likely. \u00a0The first week&#8217;s values would get reduced, and the last week&#8217;s values would get increased. \u00a0Or those values of the first week would altogether vanish from the graph. \u00a0That is unless you are careful. \u00a0Which I hope you&#8217;ll now be.<\/p>\n<p>See comments to <a href=\"http:\/\/php.net\/manual\/en\/function.date.php\">PHP date() manual<\/a> for several solutions of this problem.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>Let&#8217;s say you have a bunch of statistical data. \u00a0And all that data is date-related. \u00a0And let&#8217;s say want to display that data on a chart, a weekly average or something along those lines. \u00a0One of the ways for you to place the value into the proper week would be something like this: $week = &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2010\/02\/10\/php-date-and-53-weeks\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP date() and 53 weeks<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_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","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18],"tags":[38,74],"keyring_services":[],"class_list":["post-12125","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","tag-php","tag-tips"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":28653,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/06\/30\/php-jsonq-a-simple-elegant-php-package-to-query-over-any-type-of-json-data\/","url_meta":{"origin":12125,"position":0},"title":"php-jsonq &#8211; a simple, elegant PHP package to query over any type of JSON data","author":"Leonid Mamchenkov","date":"June 30, 2018","format":false,"excerpt":"php-jsonq provides an easy, yet powerful way to build queries for any JSON data (or PHP data structures for that matter, which are a step away).\u00a0 This has a variety of useful applications - data migration, API response filtering, complex configurations manipulation, and so on, and so forth.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":25030,"url":"https:\/\/mamchenkov.net\/wordpress\/2015\/12\/10\/weird-php-error-output-bug\/","url_meta":{"origin":12125,"position":1},"title":"Weird PHP error output bug","author":"Leonid Mamchenkov","date":"December 10, 2015","format":false,"excerpt":"We came across this PHP bug at work today. \u00a0But before you go and read it, let me show you a use case. \u00a0See, if you can spot the problem. We had a cron job script which looked something like this (shortened for clarity): [code lang=\"bash\"] #!\/bin\/bash # ... a\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":21719,"url":"https:\/\/mamchenkov.net\/wordpress\/2014\/04\/28\/faker-is-a-php-library-that-generates-fake-data\/","url_meta":{"origin":12125,"position":2},"title":"Faker is a PHP library that generates fake data","author":"Leonid Mamchenkov","date":"April 28, 2014","format":"link","excerpt":"Faker is a PHP library that generates fake data","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":21465,"url":"https:\/\/mamchenkov.net\/wordpress\/2014\/04\/09\/php-debug-bar\/","url_meta":{"origin":12125,"position":3},"title":"PHP Debug Bar","author":"Leonid Mamchenkov","date":"April 9, 2014","format":"link","excerpt":"PHP Debug Bar The DebugBar integrates easily in any projects and can display profiling data from any part of your application. It comes built-in with data collectors for standard PHP features and popular projects.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":29035,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/11\/27\/avoid-complex-arrays-in-php\/","url_meta":{"origin":12125,"position":4},"title":"Avoid complex arrays in PHP","author":"Leonid Mamchenkov","date":"November 27, 2018","format":false,"excerpt":"Now that PHP 7+ sorted out a whole bunch of problems with type-hinting of parameters, return values, variables and properties, we turn our attention to somewhat deeper issues. Array is a native citizen in PHP.\u00a0 Arrays are very convenient and are widely used.\u00a0 However, if you stop and think about\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":18605,"url":"https:\/\/mamchenkov.net\/wordpress\/2013\/09\/13\/beautifying-phps-json_encode-output\/","url_meta":{"origin":12125,"position":5},"title":"Beautifying PHP&#8217;s json_encode() output","author":"Leonid Mamchenkov","date":"September 13, 2013","format":false,"excerpt":"I've been working a bit more with PHP and JSON recently and one of the things that annoyed me quite a bit was the single line output of the json_encode() function. \u00a0Here is an example: [code lang=\"php\"] <?php $data = array( 'foo' => 'bar', 'bar' => 'qux', 'blah' => 'blah',\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/12125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=12125"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/12125\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=12125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=12125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=12125"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=12125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}