By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,105 Members | 2,569 Online
Bytes IT Community
+ 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

code green
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
  1. $start = date('Y-m-d',strtotime('-1 day'));
  2. $finish = date('Y-m-d',strtotime('-1 day'));
And month to date
Expand|Select|Wrap|Line Numbers
  1. $start = date('Y-m-01');
  2. $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
Share this Question
Share on Google+
8 Replies


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

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

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

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

Markus
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
  1. sleep(60 * 60 * 24 * 7);
  2.  
slept for a week?

;)
Aug 6 '09 #6

Dormilich
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

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

Dormilich
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

Post your reply

Sign in to post your reply or Sign up for a free account.