abdfahim Posted December 26, 2007 Share Posted December 26, 2007 Is it possible to return start date of a week? Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/ Share on other sites More sharing options...
PHP_PhREEEk Posted December 26, 2007 Share Posted December 26, 2007 date() and mktime() can do/return anything you can dream up... If you would like a more informed answer, please provide a more informed question... Need to know if you need current week, a week in the past or future, how you want it formatted, etc etc... PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423327 Share on other sites More sharing options...
abdfahim Posted December 26, 2007 Author Share Posted December 26, 2007 I want to give any week number as input and want the php to return start date of that week. Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423328 Share on other sites More sharing options...
redarrow Posted December 26, 2007 Share Posted December 26, 2007 this is the long way around but i tried..... <?php $day_now=date("D"); if($day_now=="Mon"){ $x=0; } if($day_now=="Tue"){ $x=1; } if($day_now=="Wed"){ $x=2; } if($day_now=="Thi"){ $x=3; } if($day_now=="Fri"){ $x=4; } if($day_now=="Sat"){ $x=5; } if($day_now=="Sun"){ $x=6; } $r=date("-m-y"); $first_day_in_a_week = date("d")-$x; echo " Date for start week day is: $first_day_in_a_week.$r"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423339 Share on other sites More sharing options...
PHP_PhREEEk Posted December 26, 2007 Share Posted December 26, 2007 Here's a function: <?php function getFirstInWeek ($week_no, $year) { $time = strtotime($year . '0104 +' . ($week_no - 1) . ' weeks'); $monday = strtotime('-' . (date('w', $time) - 1) . ' days', $time); return $monday; } $week_no = 52; $year = 2007; $firstDate = getFirstInWeek($week_no, $year); echo 'The first date in week ' . $week_no . ' of ' . $year . ' is the ' . date("jS \of F", $firstDate); /* Prints The first date in week 52 of 2007 is the 24th of December */ PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423341 Share on other sites More sharing options...
redarrow Posted December 26, 2007 Share Posted December 26, 2007 was mine any good like to no see no real problam just asking........ Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423346 Share on other sites More sharing options...
PHP_PhREEEk Posted December 26, 2007 Share Posted December 26, 2007 If it works, it's good. I didn't test yours, tho... and if it works, then you'd need to wrap it into a function so it can receive specific values to work with rather than the current date (which is what the OP requested...). PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423347 Share on other sites More sharing options...
redarrow Posted December 26, 2007 Share Posted December 26, 2007 ok mate thank you for your reply just studying have a good new year... PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423349 Share on other sites More sharing options...
Barand Posted December 26, 2007 Share Posted December 26, 2007 was mine any good like to no see no real problam just asking........ @redarrow You can save on all the "if" statements by using date('w') if Sunday is first day of week $x = date('w'); If Mon is first day $x = (date('w') + 6) % 7; $first_day_in_a_week = date("d")-$x; That will give problems if today is, say, 3rd and $x > 2 (--> zero or negative day !) Better to use (as PHP_Phreeek has) strtotime ("-$x days") or even just echo date('d-m-y', strtotime("last monday")); Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423382 Share on other sites More sharing options...
abdfahim Posted December 27, 2007 Author Share Posted December 27, 2007 thanx PHP_PhREEEk, your code works fine, EXCEPT that both the first date in week 1 of 2008 and week 53 of 2007 is shown as the 31st of December,2007 (which should be 53rd week of 2007)!! Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423871 Share on other sites More sharing options...
abdfahim Posted December 27, 2007 Author Share Posted December 27, 2007 Ok, From Weekipedia, I got the definition that week starting from 31 st Deecmber 2007 is Week 01 of 2008. This says every thing. Thanx all. Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423881 Share on other sites More sharing options...
PHP_PhREEEk Posted December 27, 2007 Share Posted December 27, 2007 Ok, From Weekipedia, I got the definition that week starting from 31 st Deecmber 2007 is Week 01 of 2008. This says every thing. Thanx all. Yep... nice thing about strtotime() is that it accepts what one would think is 'out of range' and makes sense of it. There are only 52 weeks per year, so 53 should break the script but it doesn't. You could ask for week 60 of 2007 and it would return 'actual' week 8 of 2008. You could add logic to the function to re-format a bit if out of ranges occur. By the way, the function will receive and calculate negative weeks (backwards) as well. = ) Onto what you've mentioned, Monday is considered the first day of a week. If the first Monday is NOT Jan 1st of that year (as it was this current year - 2007), then we have to move backwards to Dec whatever of the year prior until we hit that first Monday. You'll see why then, the initialization calendar date for the function is January 4th of any given year, because we always have to guarantee that there is a January 4th in the first week of every year (according to ISO 8601 standards). Those kinds of things will getchya if you're not careful. = ) Glad you found the function useful! PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-423915 Share on other sites More sharing options...
abdfahim Posted January 7, 2008 Author Share Posted January 7, 2008 Sorry that I read this message late, but thanx v much PHP_PhREEEk for your knowledge sharing ... this week thing was a maze to me !!! Now its clear. Quote Link to comment https://forums.phpfreaks.com/topic/83216-solved-start-date-of-a-week/#findComment-432558 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.