am_25 Posted February 20, 2007 Share Posted February 20, 2007 I need the dates of the week to be displayed, with respect to the current date. How can this be done? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 20, 2007 Share Posted February 20, 2007 <?php for($i=0; $i<7; $i++){ $date = mktime(0,0,0, date("m"), (date("d")+$i), date("Y")); print date("Y-m-d", $date); } ?> Quote Link to comment Share on other sites More sharing options...
am_25 Posted February 20, 2007 Author Share Posted February 20, 2007 thanks But one more question. Today's date is 20th tuesday. The week starts from 19th Mon to 25th Sun I want it to be displayed like that For eg. In case the date is 23rd feb, it should display from 19th to 25th How to do that? thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 20, 2007 Share Posted February 20, 2007 <?php function showWeek($startDay, $start=NULL){ if(!$start){ $start = time(); } while(date("w") != $startDay){ $start = $start-(60*60*24); } for($i=0; $i<7; $i++){ $date = mktime(0,0,0, date("m", $start), (date("d", $start)+$i), date("Y", $start)); print date("Y-m-d", $date).'<br />'; } } showWeek(1); ?> Should start on Monday. I made it a little more abstract for you. Quote Link to comment Share on other sites More sharing options...
am_25 Posted February 20, 2007 Author Share Posted February 20, 2007 thanks a lot but it does not work...I guess it is too abstract for me. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 20, 2007 Share Posted February 20, 2007 What doesn't work? Quote Link to comment Share on other sites More sharing options...
tllewellyn Posted February 20, 2007 Share Posted February 20, 2007 I'm not aware of any quick and easy function that will let you do this, but you can work it out with a bit of math. Begin by determining what the first day of the month is with something like: $firstday = date('D', mktime(0,0,0, date('m'), 1, date('Y'))); Then you set up an offset value for the first day of the week based on $firstday, using a switch: switch($firstday) { case 'Mon': $offset = 1; break; case 'Tue': $offset = 2; break; case 'Wed': $offset = 3; break; case 'Thu': $offset = 4; break; case 'Fri': $offset = 5; break; case 'Sat': $offset = 6; break; case 'Sun': $offset = 7; break; } Then you can establish the startday of each week: $week1 = 1 + $offset; $week2 = $week1 + 7; $week3 = $week2 + 7; $week4 = $week3 + 7; You will have to further refine it for the end of the month, but this should be enough to get you going. Good luck! Trev Quote Link to comment Share on other sites More sharing options...
am_25 Posted February 21, 2007 Author Share Posted February 21, 2007 This code works well. $StartOfWeek = date("Y-m-d",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y"))); $EndOfWeek = date("Y-m-d",mktime(23,59,59,date("n"),(date("j")+(6-date("w"))),date("Y"))); echo $StartOfWeek; print "<br>"; echo $EndOfWeek; Thanks all. Quote Link to comment Share on other sites More sharing options...
am_25 Posted February 21, 2007 Author Share Posted February 21, 2007 with this code how to get the dates for the entire week $StartOfWeek = date("Y-m-d",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y"))); $EndOfWeek = date("Y-m-d",mktime(23,59,59,date("n"),(date("j")+(6-date("w"))),date("Y"))); echo $StartOfWeek; print "<br>"; echo $EndOfWeek; Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 Again, what didn't work with my code? Quote Link to comment Share on other sites More sharing options...
am_25 Posted February 21, 2007 Author Share Posted February 21, 2007 I just copied and ran your code, it didnot show up anything. I'm not sure what went wrong. Thanks again Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 http://www.grady.us/temp.php 2007-02-19 2007-02-20 2007-02-21 2007-02-22 2007-02-23 2007-02-24 2007-02-25 Looks fine to me... I changed one line, but it still worked fine before that, it just wouldn't have in the event you sent a different start date: while(date("w", $start) != $startDay){ Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 21, 2007 Share Posted February 21, 2007 thanks But one more question. Today's date is 20th tuesday. The week starts from 19th Mon to 25th Sun That can begin to be a bit tricky since the week realistically starts on Sunday and runs through Saturday. I would recommend something like this as an alternative to jesi's code, if you are only after displaying the current week: <?php function displayWeek() { if (date('w') == 0) { // today is sunday, so get LAST monday's date to start with $start = strtotime("last Monday"); } else { // this monday will give us the right starting date $start = strtotime("Monday"); } $dates = array(); // Loop for 7 days for ($i = 0; $i < 7; $i++) { $dates[] = date('Y-m-d', strtotime("$start +{$i} days")); } return $dates; } $dates = displayWeek(); echo implode("<br />", $dates); ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 Thanks Obsidian, I always forget about how strtotime can handle stuff like "Last Monday" - that makes it cleaner and shorter than mine. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 $start = strtotime("last Monday"); ??? i never know it does that, what other text does it read? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 I dunno Ted, maybe the manual has a list of the syntax it supports... Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 21, 2007 Share Posted February 21, 2007 I dunno why I see a contradiction between: I dunno Ted, maybe the manual has a list of the syntax it supports... AND Thanks Obsidian, I always forget about how strtotime can handle stuff like "Last Monday" - that makes it cleaner and shorter than mine. Can any body suggest why that is :-\ Ted Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 21, 2007 Share Posted February 21, 2007 $start = strtotime("last Monday"); ??? i never know it does that, what other text does it read? Test it and see... I've been amazed at the different things I've been able to use it for: strtotime — Parse about any English textual datetime description into a Unix timestamp Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 21, 2007 Share Posted February 21, 2007 I dunno why I see a contradiction between: I dunno Ted, maybe the manual has a list of the syntax it supports... AND Thanks Obsidian, I always forget about how strtotime can handle stuff like "Last Monday" - that makes it cleaner and shorter than mine. Can any body suggest why that is :-\ Ted LOL... just remember there's a difference between being able to spout off a list of all available syntax formats for a function and not remembering one that you learned a while back Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 Because I didn't ask other people a question that can be answered by looking in the manual - I simply forgot how to do something which was available and did it another way. You asked us for information which was available, you didn't post another way to do it. Edit: plus what Obsidian said. Quote Link to comment Share on other sites More sharing options...
am_25 Posted February 22, 2007 Author Share Posted February 22, 2007 Thanks all it works. Jesirose special thanks your code works.. I tried it today Quote Link to comment 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.