note 129945 added to datetime.format

From: Date: Wed, 08 Jan 2025 09:12:14 +0000
Subject: note 129945 added to datetime.format
Groups: php.notes 
Request: Send a blank email to php-notes+get-221758@lists.php.net to get a copy of this message
If you want to get the week of year + year of said week, you need to use `format('o-W'), otherwise you can stumble into a non-obvious gotcha (unless you RTFM and memorised it, that is). Using Y instead of o can result in incorrect year values in the case of the first or last week of the year (depending on if January 4th falls into said week or not), such as the first week of 2025 between 2024-12-30 and 2025-01-05 - (new DateTime('2024-12-30'))->format('o-W') will return the correct value of 2025-01 (as per ISO-8601 definition of week of year), while format('Y-W') will return 2024-01. Because of this, I would personally recommend avoiding using week of year anywhere unless absolutely necessary, as it is easy to make this mistake and never realise it. ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 109.110.4.79) ---- Manual Page -- https://php.net/manual/en/datetime.format.php Edit -- https://main.php.net/note/edit/129945 Del: integrated -- https://main.php.net/note/delete/129945/integrated Del: useless -- https://main.php.net/note/delete/129945/useless Del: bad code -- https://main.php.net/note/delete/129945/bad+code Del: spam -- https://main.php.net/note/delete/129945/spam Del: non-english -- https://main.php.net/note/delete/129945/non-english Del: in docs -- https://main.php.net/note/delete/129945/in+docs Del: other reasons-- https://main.php.net/note/delete/129945 Reject -- https://main.php.net/note/reject/129945 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221758) next »