vacek Posted March 13, 2014 Share Posted March 13, 2014 Hi, I'm in need of a bit of help with the following. Right now, I'm using the following to get the date on Sunday and Saturday of the current week. $startweek = date('m-d-Y',time()+( 0 - date('w'))*24*3600); $endweek = date('m-d-Y',time()+( 6 - date('w'))*24*3600); For the current week, this will return: 03-09-2014 & 03-15-2014 This works great for the current week. However I would like to pass it a variable ($weeknum) and have it calculate the dates of Sunday and Saturday of that particular week. I know that this ( $date=date("W"); ) will return the current week number (although flawed for me as it is Monday -> Sunday I believe.) I have tried the following, but the results are not what I expected. They are a week before the week I need. $weeknum = date('W'); $startweek = date('m-d-Y',time()+( 0 - $weeknum)*24*3600); $endweek = date('m-d-Y',time()+( 6 - $weeknum)*24*3600); So 2 things I clearly need: 1.) A way to get the current week #, Sunday ->Saturday 2.) The dates of the current week, Sunday & Saturday .. when the $weeknum is passed to it. Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/286952-need-help-with-dates-sun-sat/ Share on other sites More sharing options...
requinix Posted March 14, 2014 Share Posted March 14, 2014 It depends on how you define weeks - particularly around the boundaries. Consider December 31st, 2013 (a Tuesday) and January 1st, 2014 (a Wednesday). What week numbers do they get? a) 2013-W52 and 2013-W52 because the week started in 2013? January's is a bit odd. b) 2013-W52 and 2014-W01 because of their respective years? Neither week has seven days: three for 2013-W52 and four for 2014-W01. c) 2014-W01 and 2014-W01 because the week range contained January 1st? December's is a bit odd. (This is how 'W' works.) Quote Link to comment https://forums.phpfreaks.com/topic/286952-need-help-with-dates-sun-sat/#findComment-1472529 Share on other sites More sharing options...
vacek Posted March 14, 2014 Author Share Posted March 14, 2014 Great points ... As long as I can get 95% of the year right, I'll be good. Most important for me, is to get the dates in any particular week Sunday to Saturday ... (week number not as important, and I can just not use that at all) I just need them to show up on the webpage automatically each week. Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/286952-need-help-with-dates-sun-sat/#findComment-1472543 Share on other sites More sharing options...
Solution requinix Posted March 14, 2014 Solution Share Posted March 14, 2014 As long as I can get 95% of the year right, I'll be good. Please, no. That mentality causes so many problems with software and I, for one, find it incredibly frustrating when people use it. Most important for me, is to get the dates in any particular week Sunday to Saturday ... (week number not as important, and I can just not use that at all)That's actually what I thought, but you kept talking about the week number. So you've got a date and you want to know the previous Sunday and the next Saturday? The very first bit of code you posted was correct-ish - just had to make time() a variable. $date = strtotime("2014-01-01"); // for example // break out the parts of the date list($y, $m, $d, $w) = explode("-", date("Y-m-d-w", $date)); // sunday is w days ago $sunday = mktime(0, 0, 0, $m, $d - $w, $y); // saturday is (6 - w) days in the future $saturday = mktime(0, 0, 0, $m, $d + 6 - $w, $y);Adding seconds, like you had with 24*3600, is imprecise because not all days have 86400 seconds. Plus daylight savings can give you unexpected results. Quote Link to comment https://forums.phpfreaks.com/topic/286952-need-help-with-dates-sun-sat/#findComment-1472548 Share on other sites More sharing options...
vacek Posted March 14, 2014 Author Share Posted March 14, 2014 Thanks a MILLION .... Quote Link to comment https://forums.phpfreaks.com/topic/286952-need-help-with-dates-sun-sat/#findComment-1472605 Share on other sites More sharing options...
vacek Posted March 15, 2014 Author Share Posted March 15, 2014 So i took a swag at implementing this .. something is not right: $date = date('d.m.Y',strtotime("2014-01-01")); // for example // break out the parts of the date list($y, $m, $d, $w) = explode("-", date("Y-m-d-w", $date)); // sunday is w days ago $sunday = date('d.m.Y', mktime(0, 0, 0, $m, $d - $w, $y)); // saturday is (6 - w) days in the future $saturday = date('d.m.Y',mktime(0, 0, 0, $m, $d + 6 - $w, $y)); My results using the code as above are: 01.01.201428.12.196903.01.1970 Thoughts ? Quote Link to comment https://forums.phpfreaks.com/topic/286952-need-help-with-dates-sun-sat/#findComment-1472663 Share on other sites More sharing options...
vacek Posted March 15, 2014 Author Share Posted March 15, 2014 sorry, was self inflicted This code works: $date = strtotime("2014-01-01"); // for example // break out the parts of the date list($y, $m, $d, $w) = explode("-", date("Y-m-d-w", $date)); // sunday is w days ago $sunday = mktime(0, 0, 0, $m, $d - $w, $y); // saturday is (6 - w) days in the future $saturday = mktime(0, 0, 0, $m, $d + 6 - $w, $y); $date1 = date('d.m.Y', $date); $sunday1 = date('d.m.Y', $sunday); $saturday1 = date('d.m.Y', $saturday); echo $date1.'<BR>'; echo $sunday1.'<BR>'; echo $saturday1; 01.01.201429.12.201304.01.2014 Quote Link to comment https://forums.phpfreaks.com/topic/286952-need-help-with-dates-sun-sat/#findComment-1472665 Share on other sites More sharing options...
requinix Posted March 15, 2014 Share Posted March 15, 2014 Right. That value for $date is supposed to be a number like the one you get from strtotime() or mktime() - a number is much easier to work with than some kind of year-month-day string. Quote Link to comment https://forums.phpfreaks.com/topic/286952-need-help-with-dates-sun-sat/#findComment-1472669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.