473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get dates Saturday to Friday previous

code green
1,726 Expert 1GB
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
8 3948
code green
1,726 Expert 1GB
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
8,658 Expert Mod 8TB
@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
1,726 Expert 1GB
Is the server request more efficient than the function call Dormilich?
Aug 6 '09 #4
Dormilich
8,658 Expert Mod 8TB
@code green
at least the Zend people say so (but it does make sense).
Aug 6 '09 #5
Markus
6,050 Expert 4TB
@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
8,658 Expert Mod 8TB
@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
6,050 Expert 4TB
@Dormilich
It was a joke ;)
Aug 6 '09 #8
Dormilich
8,658 Expert Mod 8TB
@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

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

Similar topics

26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
8
by: netsurfer | last post by:
Hi: Have a question on making the date automatically filled in by what the user enters in by the date at the top. The date entered at the top would most likely be on a Wednesday then I need...
2
by: rf | last post by:
Hey I need to be able to get the dates from last wednesday to the past tuesday on thursday or friday every week. How would I do that? For example: m t W TH F M T w th f I would need the...
2
by: planetthoughtful | last post by:
Hi All, I'm building some reports in Acc97 and using a custom calendar form to allow users to pick dates with which to report. I'm wondering if there's an easy way in code to be able to...
2
by: MLH | last post by:
With a table of holidays and A97's date fn's - how best to count weekends and holidays between two dates? My holiday table has 4 fields. I will be adding records to it each year as info becomes...
2
by: David | last post by:
Hi, I need to display 3 fields with future dates (Fridays only) as table column headers. i.e. 16th Feb | 23rd Feb | 2nd March. As each week passes, i.e. from the Saturday, the dates should...
9
by: clintonb | last post by:
I'm looking for a way to calculate the number of days between two dates using standard C++ functions. Would it be as simple as just using the difftime() function and then dividing that result by...
4
by: jdph40 | last post by:
I enter data into a database that resides on our network. Other employees can print reports from the database, but they have no way of telling if I've entered all the data. For instance, on...
5
by: cla | last post by:
I'm using this code on an application to track football schedules: ---- $season = '2005'; $basedate = strtotime('this friday', strtotime('31 August '.$season)); for($d=0;$d<=31;$d++) {...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.