Calculating start and end dates of a week.
A friend asked "How do I calculate start (monday) and end (sunday) dates from a given week number for a specified year?" Instead of having to come up with your own algorithm you can simply do the following in PHP 5.1 and higher:
<?php
// Monday
echo date(
datetime::ISO8601,
strtotime("2006W37"));
// Sunday
echo date(
datetime::ISO8601,
strtotime("2006W377"));
?>
The format is " year 'W' weeknr ( daynr )?" where the default daynr is 1 being the Monday of that week. The daynr can be in the range 0 to 7. The weeknr is the ISO week number . Please note that the " year '-W' weeknr ( '-' daynr )?" format is only supported in PHP 5.2 and higher.
Comments
Thanks Derek, that is going to make life a lot easier!
Keep it simple and just do strtotime('-1 Sunday',$currentDay);
it is simpler and uses less resources.
or you could use PEAR::Date(), it makes it very easy as well
@Ammar: Why would you want to use a wrapper class in PHP when the extension supports it?
Hey Dereck
thanks for confirming yet again why I love the Internet... I've been playing around with this particular problem for the last few days... any solution I had was not nearly as elegant and simple as this... glad I found this posting...will help tremendously.
echo date("Y-m-d", strtotime(date("Y").'W'.date('W')."7"));
If 1 for daynr represents monday, what does 0 represent? How can it accept values 0-7?
Thnx! Just what I was looking for!
Thank you very much!
Excellent example of exactly what a lot of people need. Please keep this example up for the rest of the PHP newbs like me!
You have just saved me a lot of time on deadline day at work.
Thanks!
Thank you very much. This threat was my first search result: "php calculate start end week"..
Wow. I had no clue about this. Thanks, man!
Very very usefull, I knew about the W weeks implemented in recent php but this is pretty useful, saved me on a thing I have to do at work :)
Excellent...!
Exactly what I was looking for.
Even in 2013 this is very useful, thanks! Saved me much work
Thank you! I was getting very frustrated trying to get my dates to format properly, you're a lifesaver!
Life Line
Merged pull request #1029
Reflow some comments
Add comments, add end of file newlines, fix php 8.5 compilation
Benchmark Xdebug performance
Merged pull request #1051
PHP 8.6: printf() is now optimised out if it only uses %s and %d (and…
PHP 8.6: The object IDs of objects changed in tests
PHP 8.6: ZSTR_INIT_LITERAL() no longer takes non-literals, so use zen…
PHP 8.6: WRONG_PARAM_COUNT has been removed in favour of zend_wrong_p…
PHP 8.6: zval_dtor() has been deprecated in favour of zval_ptr_dtor_n…
Update test for version constraints, as well as the error messages
I just watched an interesting and lovely documentary on BBC Four: The Magical World of Moss.
It's on the iPlayer for another month:
https://www.bbc.co.uk/programmes/m001hqthMerge branch 'xdebug_3_5'
Document to use PIE instead of PECL in the README
If we must show off a little…
In more info innocent times, the hat riots caused some spankings...
100 Years Ago Men and Boys Fought on the Streets of New York Over Wearing Straw Hats Past Summer | The New York Public Library
https://www.nypl.org/blog/2022/09/23/straw-hat-riots-nycI hiked 15.5km in 3h12m14s
📷 Mushroom Village
🚩 Weg van den Prins Willemsberg, Ellecom, Nederland
I walked 2.4km in 59m34s
📷 National Cycle Network 6
🚩 Watford, United Kingdom




Shortlink
This article has a short URL available: https://drck.me/csaedoaw-4wr