deiffert Posted January 16, 2008 Share Posted January 16, 2008 I'm trying to get the week of the month for 2 weeks ago I am able to get the current week of the month (1 through 4 or 5) like this: $weekNum = date("W") - date("W",strtotime(date("Y-m-01"))) + 1; and I know I can get info for 2 weeks ago like this: strtotime('2 weeks ago') but I can't figure out how to combine them. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/86334-strtotime-help/ Share on other sites More sharing options...
GingerRobot Posted January 16, 2008 Share Posted January 16, 2008 Personally i wouldn't bother using strtotime() - i'd just take away the number of seconds in two weeks from the current time: <?php $weekNum = date("W",time()-60*60*24*14); echo $weekNum; ?> Also, you don't need all that to get the current week number. This would suffice: <?php $weekNum = date("W"); ?> Edit: misread this - didn't realise you were asking for the week of the month Quote Link to comment https://forums.phpfreaks.com/topic/86334-strtotime-help/#findComment-441136 Share on other sites More sharing options...
deiffert Posted January 16, 2008 Author Share Posted January 16, 2008 Thanks for the response. It seems like that's giving me the week number (1-52) whereas I'm looking for the week of the month (1-4 or 5) So it gets tricky because if I want the week of the month for 2 weeks ago, I can't just subtract 2 because I need it to go into the previous month (if 2 weeks ago was in the previous month). Does that make any sense? Quote Link to comment https://forums.phpfreaks.com/topic/86334-strtotime-help/#findComment-441144 Share on other sites More sharing options...
GingerRobot Posted January 16, 2008 Share Posted January 16, 2008 Ok, well this would do it based on a date: <?php $d = '29'; $m = '01'; $y = '2008'; $diff = strtotime("$m/$d/$y") - strtotime("$m/01/$y"); $weeks_diff = floor($diff/(60*60*24*7))+1;//rounding down and adding 1 to allow for the date being the 1st of the month //rounding up would return 0 echo $weeks_diff; ?> So we just need to use date() to generate the date: <?php $d = date('d',time()-60*60*24*14); $m = date('m',time()-60*60*24*14); $y = date('Y',time()-60*60*24*14); $diff = strtotime("$m/$d/$y") - strtotime("$m/01/$y"); $weeks_diff = floor($diff/(60*60*24*7))+1; echo $weeks_diff; ?> Quote Link to comment https://forums.phpfreaks.com/topic/86334-strtotime-help/#findComment-441157 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.