onlyican Posted September 18, 2006 Share Posted September 18, 2006 Hey guysThis is where I normally build a function to do this, and someone says, U can use X();So I thought I would ask firstWhat I want, is the script to work out the date this weekFor exampleThe date today is 18/09/06 (Monday)I want the script to work out the date FROM and To, Always starting on Monday and Ending on SundayAs its monday today, The script should work outFROM: 2006/09/18 (MySQL Date, Y-m-d)TO: 2006/09/04Its easy if its always monday, but on Wednesday (would be 2006/09/20)I want the same dates to showIf there aint a function, I would prob get todays Day, work out how many days between today and Monday (working Backwards)then the days to SundayThen if either one rolls over a new month, or evan year, to work it outAnyone got better ideas? Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 18, 2006 Share Posted September 18, 2006 I would recommend something like this function. AFAIK, there's not a [i]built-in[/i] way to retrieve what you're after, but something like this will work nicely. Notice that with the initial [i]if[/i] statement, i'm determining what monday to start your count from, so you don't have to work two different directions, you can simply start from Monday and increment through Sunday:[code]<?php// returns an array of all dates from Monday - Sundayfunction getThisWeek() { if (date('w') == 0) $first = strtotime("last monday"); else $first = strtotime("this monday"); $day = 60 * 60 * 24; // one day $last = $first + ($day * 6); // add six days $week = array(); while ($first <= $last) { $week[] = date('Y-m-d', $first); $first += $day; // increment by one day } return $week;}$week = getThisWeek();echo "<pre>\n";var_dump($week);echo "</pre>\n";?>[/code]that function will let you call the first and last records in the returned array to get the Monday and Sunday dates that you're after. hope that helps Quote Link to comment Share on other sites More sharing options...
onlyican Posted September 18, 2006 Author Share Posted September 18, 2006 Exactly what I am on aboutI did not know I could use strtotime like that ("last monday") ectWicked thanks Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 18, 2006 Share Posted September 18, 2006 [quote author=onlyican link=topic=108488.msg436489#msg436489 date=1158586615]Exactly what I am on aboutI did not know I could use strtotime like that ("last monday") ectWicked thanks[/quote]glad to help. to get a better feel for the incredible flexibility of what strtotim() will accept, check out [url=http://www.gnu.org/software/tar/manual/html_node/tar_109.html]the GNU date input formats[/url]. this is the guidelines that the strtotime() function follows. Quote Link to comment Share on other sites More sharing options...
onlyican Posted September 18, 2006 Author Share Posted September 18, 2006 Cheers, I will look at that (well its bookmarked with another 100 links to catch up on)I am a programmer, I dont have time, I eat one handed while working with the other hand (on the keyboard now people)One more questionI have forgotten how to find out the Day from a Manually typed dateFor example, if i type in 2006-7-28How can the code work out what Day that was on? Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 18, 2006 Share Posted September 18, 2006 [quote author=onlyican link=topic=108488.msg436505#msg436505 date=1158587354]I have forgotten how to find out the Day from a Manually typed dateFor example, if i type in 2006-7-28How can the code work out what Day that was on?[/quote]totally understand about the time. i'm in the same boat with you on this one :P ... here's a simple way to check what day it is:[code]<?php$date = '2006-7-28';// textual day of the week:echo date('l', strtotime($date));// numeric (0 = Sunday through 6 = Saturday)echo date('w', strtotime($date));?>[/code]good luck Quote Link to comment Share on other sites More sharing options...
LAX Posted September 18, 2006 Share Posted September 18, 2006 you can use the split functionlets say that you have a textbox linked to a variable called text...You can use the split in this case like this[code=php:0]$date = split('-', $text);//this will make $date an array spited in 3 values//After the split you can manipulate each value like this:2006-7-28echo "Current Year:$date[0]";echo "Current Month:$date[1]";echo "Current Day:$date[2]";[/code] Quote Link to comment Share on other sites More sharing options...
onlyican Posted September 18, 2006 Author Share Posted September 18, 2006 But how do I find out what DAY that is? Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 18, 2006 Share Posted September 18, 2006 [quote author=onlyican link=topic=108488.msg436536#msg436536 date=1158589669]But how do I find out what DAY that is?[/quote]check out my last post... Quote Link to comment Share on other sites More sharing options...
onlyican Posted September 18, 2006 Author Share Posted September 18, 2006 soz, didn't see that, I only saw the post by lax RE explode 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.