435,105 Members | 2,569 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,105 IT Pros & Developers. It's quick & easy.

# Get dates Saturday to Friday previous

 Expert 100+ P: 1,726 I need to generate the date for the latest Friday and the Saturday previous to this ie 7 days. I am generating reports for the periods Yesterday, Month to date and Sat-Fri so I create the dates in PHP and pass them to a function to be used in the BETWEEN phrase of a query. This could run almost any day of the week but most probably the Friday itself. I have figured it out for yesterday Expand|Select|Wrap|Line Numbers \$start = date('Y-m-d',strtotime('-1 day')); \$finish = date('Y-m-d',strtotime('-1 day')); And month to date Expand|Select|Wrap|Line Numbers \$start = date('Y-m-01'); \$finish = date('Y-m-d'); But I also need \$start to be Saturdays date and \$finish to be Friday. I probably need one of date('D') date('l') or date('N') which return text or numeric representations of the day of the week but can't figure out how Aug 5 '09 #1
8 Replies

 Expert 100+ P: 1,726 Well here is the solution Expand|Select|Wrap|Line Numbers \$fri = 5; \$stmp = time(); #Get current timestamp \$day = date('N',\$stmp); #Get numeric day of week  while(\$day != \$fri) #Match to Friday {     \$stmp = \$stmp - 86400; #subtract i day     \$day = date('N',\$stmp); #Get numeric day of week  } \$finish = date('Y-m-d',\$stmp);#Fridays date \$start = date('Y-m-d',\$stmp - (86400*7)); #7 days previous   Thanks for all the help ;) Aug 5 '09 #2

 Expert Mod 5K+ P: 8,639 @code green the same without a function call: Expand|Select|Wrap|Line Numbers \$stmp = \$_SERVER['REQUEST_TIME']; I love to help. ;) Aug 5 '09 #3

 Expert 100+ P: 1,726 Is the server request more efficient than the function call Dormilich? Aug 6 '09 #4

 Expert Mod 5K+ P: 8,639 @code green at least the Zend people say so (but it does make sense). Aug 6 '09 #5

 Expert 5K+ P: 6,050 @Dormilich But REQUEST_TIME is set when a request is made to the SAPI, and never updated through the lifetime of the script - that could lead to inconsistent results if you, say: Expand|Select|Wrap|Line Numbers sleep(60 * 60 * 24 * 7);   slept for a week? ;) Aug 6 '09 #6

 Expert Mod 5K+ P: 8,639 @Markus well, most scripts finish within a second. I don't understand your example... but most of the time you just need a current timestamp. and yes, there may be cases where it could be problematic, though I consider code green smart enough to realise that. Aug 6 '09 #7

 Expert 5K+ P: 6,050 @Dormilich It was a joke ;) Aug 6 '09 #8

 Expert Mod 5K+ P: 8,639 @Markus at least better than what I'm up to now (determine the LCD (Limiting Current Density)--another boring 2 or 3 hours of waiting and measuring) Aug 6 '09 #9