Get week of month php
I have an array of random dates (not coming from MySQL). I need to group them by the week as Week1, Week2, and so on upto Week5. Show What I have is this:
What I need is a function to get the week number of the month by providing the date. I know that I can get the weeknumber by doing
I should be able to get the week Week1 by just providing the date e.g.
Anders 7,9479 gold badges50 silver badges80 bronze badges asked Sep 16, 2015 at 18:18
1 I think this relationship should be true and come in handy:
We also need to make sure that "overlapping" weeks from the previous year are handeled correctly - if January 1st is in week 52 or 53, it should be counted as week 0. In a similar fashion, if a day in December is in the first week of the next year, it should be counted as 53. (Previous versions of this answer failed to do this properly.)
To get weeks that starts with sunday, simply replace answered Sep 17, 2015 at 7:29
AndersAnders 7,9479 gold badges50 silver badges80 bronze badges 8 You can use the function below, fully commented:
answered Sep 17, 2015 at 6:52
Matteo TassinariMatteo Tassinari 17.6k7 gold badges60 silver badges80 bronze badges 4 The corect way is
answered Nov 18, 2015 at 6:38
GoendgGoendg 1222 silver badges8 bronze badges 1 I have created this function on my own, which seems to work correctly. In case somebody else have a better way of doing this, please share.. Here is what I have done.
answered Sep 17, 2015 at 7:22
Asif HussainAsif Hussain 3411 gold badge2 silver badges11 bronze badges 2
Goendg solution does not work for 2016-10-31.
answered Jun 10, 2016 at 16:48
j4r3kj4r3k 1295 bronze badges
weekOfMonth ('2015-09-17') // returns 3 answered Jun 7, 2018 at 7:10
1 Given the time_t wday (0=Sunday through 6=Saturday) of the first of the month in
Translated into PHP:
answered Apr 26, 2016 at 2:58
Mark ReedMark Reed 87.5k15 gold badges134 silver badges167 bronze badges You can also use this simple formula for finding week of the month
ALGORITHM : Date = '2018-08-08' => Y-m-d
answered Aug 8, 2018 at 10:38
My function. The main idea: we would count amount of weeks passed from the month's first date to current. And the current week number would be the next one. Works on rule: "Week starts from monday" (for sunday-based type we need to transform the increasing algorithm)
answered May 6, 2019 at 0:04
working fine. answered Jun 25, 2021 at 8:29
answered Jul 16, 2021 at 16:21
BalajiBalaji 271 silver badge7 bronze badges
Short variant:
answered Sep 26, 2018 at 14:33
3 There is a many solutions but here is one my solution that working well in the most cases.
It accept unix timestamp, normal date or return current week from the Enjoy! answered Sep 3, 2019 at 13:45
I know this an old post but i have an idea!
answered Dec 2, 2019 at 15:25
1
answered Feb 11, 2020 at 8:26
How about this function making use of PHP's relative dates? This function assumes the week ends on Saturday. But this can be changed easily.
answered Feb 17, 2021 at 16:27
Chin. UdaraChin. Udara 6003 silver badges17 bronze badges This function returns the integer week number of the current month. Weeks always start on Monday and counting always starts with 1.
Example of use
A test for all days from 1900-2038 with the accepted solution from @Anders as a reference:
Result: 50405 compare ok, 0 errors answered Jul 13, 2021 at 16:15
jspitjspit 6,0411 gold badge7 silver badges15 bronze badges I took the visual approach (like how we do it in the real world). Instead of using formulas or what not, I solved it (or at least I think I did) by visualizing a literal calendar and then putting the dates in a multidimensional array. The first dimension corresponds to the week. I hope someone can check if it stands your tests. Or help someone out with a different approach.
answered Aug 3, 2021 at 16:48
PaoloPaolo 3702 gold badges4 silver badges20 bronze badges 2
Anders 7,9479 gold badges50 silver badges80 bronze badges answered Jul 2, 2017 at 13:44
1 Not the answer you're looking for? Browse other questions tagged php arrays date week-number or ask your own question.How to Find week of month in PHP?Date = '2018-08-08' => Y-m-d. Find out day of the month eg. ... . Find out Numeric representation of the day of the week minus 1 (number of days in week) eg. ( 3-1). Take difference and store in result.. Subtract 1 from result.. Divide it by 7 to result and ceil the value of result.. Add 1 to result eg.. How do I calculate the number of weeks between two dates in PHP?php function week_between_two_dates($date1, $date2) { $first = DateTime::createFromFormat('m/d/Y', $date1); $second = DateTime::createFromFormat('m/d/Y', $date2); if($date1 > $date2) return week_between_two_dates($date2, $date1); return floor($first->diff($second)->days/7); } $dt1 = '1/1/2014'; $dt2 = '12/31/2014'; ...
|