golles Posted May 12, 2008 Share Posted May 12, 2008 Hey guys, I have some experience with PHP, but this is something I can't figure out, not even with google... I want to get the date of the monday of a know week and year so for example 20 and 2008 would return 5-12-2008, 19 and 2008 would return 5-5-2008 Hopefully someone can help me kind regards, golles Quote Link to comment https://forums.phpfreaks.com/topic/105320-solved-first-date-of-a-known-week/ Share on other sites More sharing options...
GingerRobot Posted May 12, 2008 Share Posted May 12, 2008 Something like this should work: <?php $weekno='19'; $year='2008'; echo date('d-m-y',strtotime('last monday',strtotime('01/01/'.$year)+$weekno*60*60*24*7+60*60*24)); Basically, you get the time of the beginning of the year, then add on the number of seconds in the number of weeks required. You then add on one more day, otherwise a year which started on a monday would give odd results. Finally, you format it how you want it. If you're not quite sure what i mean by needing to add an extra day, then think about running this today: <?php echo date('d-m-y',strtotime('last monday')); ?> This would give the monday of last week's date - when in fact we would want today. Oh, and there is also the question of what you consider to be the first week of the year. For example, 2008 began on a Tuesday - so is the first week of the year beginning of the 7th January, or would you count that as the second week? Quote Link to comment https://forums.phpfreaks.com/topic/105320-solved-first-date-of-a-known-week/#findComment-539349 Share on other sites More sharing options...
Barand Posted May 12, 2008 Share Posted May 12, 2008 try <?php $y = 2008; $w = 20; $dw = ($w-1)*7; // days to week w $wk20 = strtotime("+$dw days", mktime(0,0,0,1,4,$y)); // date in week 20 (wk 1 is week containing jan 4) $dow = (date('w', $wk20) + 6) % 7; // day of week converted to monday = 0 $mon = date('m-d-Y', strtotime("-$dow days", $wk20)); // get monday of week containing $wk20 echo $mon; // 5-12-2008 ?> Quote Link to comment https://forums.phpfreaks.com/topic/105320-solved-first-date-of-a-known-week/#findComment-539383 Share on other sites More sharing options...
golles Posted May 13, 2008 Author Share Posted May 13, 2008 try <?php $y = 2008; $w = 20; $dw = ($w-1)*7; // days to week w $wk20 = strtotime("+$dw days", mktime(0,0,0,1,4,$y)); // date in week 20 (wk 1 is week containing jan 4) $dow = (date('w', $wk20) + 6) % 7; // day of week converted to monday = 0 $mon = date('m-d-Y', strtotime("-$dow days", $wk20)); // get monday of week containing $wk20 echo $mon; // 5-12-2008 ?> That was what I needed! thank you! Quote Link to comment https://forums.phpfreaks.com/topic/105320-solved-first-date-of-a-known-week/#findComment-539731 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.