Jump to content

working with dates


jakebur01

Recommended Posts

I need to do some reporting and I need some help.  The reporting periods are as follows:

 

08/28-09/03

09/04-09/10

09/11-09/17

09/18-09/24

09/25-10/01

 

So, they are Sunday through Saturday throughout each month.  Say the date is Sept. 7.... How can I find out the date of the Sunday before last.  How can I know what month I am reporting for?

 

I am querying the database each Wed. for sales data from the previous month.  I want to take the totals from my query and store them in a separate table.  The problem is, I don't know how to find out what month the previous week falls into.  And I don't know how to find the date of the Sunday before last.

 

Thanks,

 

Jake

Link to comment
Share on other sites

To get the previous Sunday (which might be today),

function lastsunday($date = null) {
if ($date === null) $date = time();
if (!ctype_digit($date)) $date = strtotime($date);
return strtotime("-" . date("w", $date) . " days", $date);
}

Tips:

1. If you want the Sunday between 1 and 8 days ago (ie, if today is Sunday then use the week before's Sunday), give $date=yesterday.

2. Use strtotime to subtract 7 more days if you want the Sunday before.

 

In general, if you need to know something about a date then use date, and if you want to travel forward or backward in time from a date then use strtotime().

Link to comment
Share on other sites

Sample Structure:

Id (int 11), Sunday (date), Saturday (date)

 

Sample Data:

0   2011-08-28   2011-09-03
1   2011-09-04   2011-09-10

 

Let say as an example the table above is filled with every week this year.  How could I query it for any month of the year. If I wanted to query it for April 2011, how could I do that and it contain all the weeks that fall within that month?  Every Sunday - Saturday period within any month I pick.

 

I can find the previous Sunday, that as not a big of a concern as being able to do this.

 

___________________________

 

If it would be hard to query the sample table.  I could store the data a different way maybe?  Calculate what month the previous week is in before I store it and use a table like this:

Id, Month, Year, Sunday, Saturday

 

0   04   2011   2011-08-28   2011-09-03

 

 

Link to comment
Share on other sites

Let say as an example the table above is filled with every week this year.  How could I query it for any month of the year. If I wanted to query it for April 2011, how could I do that and it contain all the weeks that fall within that month?  Every Sunday - Saturday period within any month I pick.

 

The problem is I don't know how to retrieve the info above.  I have the dates, but I don't know how to retrieve it.  It is not going to have a html form field to select date periods.  I want it to automatically select the previous months weeks.  So,  like today if I run the script I want it to know to retrieve the previous month which is September and know what dates to select in september.... which would be 08/28 - 09/03.

Link to comment
Share on other sites

Here's some code which produces an array with the 2 dates... Was that what you requested? Your question was vary vague.

 

$dates = array(
                date('Y-m-d', strtotime('last Sunday', strtotime('last Saturday', strtotime('last month')))),
	date('Y-m-d', strtotime('last Saturday', strtotime('last month')))
      );

 

The only problem is if you're checking this on a 29th, 30th or 31 and in some months it will skip a month (-30 days) dunno how you solve that. For example, March 31 - 30 days is 1 March.

Link to comment
Share on other sites

Let me be more specific.  Look at the table.

 

table

Id   Saturday   Sunday
0   2011-07-31   2011-08-06
1   2011-08-07   2011-08-13
2   2011-08-14   2011-08-20
3   2011-08-21   2011-08-27
4   2011-08-28   2011-09-03
5   2011-09-04   2011-09-10
6   2011-09-11   2011-09-17
7   2011-09-18   2011-09-24
8   2011-09-25   2011-10-01
9   2011-10-02   2011-10-08

 

If the current date is 2011-10-07,  how could I use php to query the table for last months periods (which are Sat. 8/28 - Sun. 10/01)?

 

How can I get php to know how my week periods are setup?  I could set up a separate table and store the period range for each month, but I would rather have the whole thing do it automatically if I could.

 

Take a look at how our periods are set up below.  It will help if you look on a calendar to see what I am talking about.

 

Jan.   1/2-1/29
Feb.   1/30-2/26
Mar.   2/27-3/26
Apr.   3/27-4/30
May   5/1-5/28
Jun.   5/29-6/25
July    6/26-7/30
Aug.   7/31-8/27
Sept.  8/28-10/1

 

Thanks.

Link to comment
Share on other sites

That is correct.

 

It takes today's date. Then subtracts 30 days. Then returns the previous Saturday from this date. The second date is the previous Sunday from this new date.

 

Want to find out the last friday of the previous month?

 

echo date('Y-m-d', strtotime('last Friday', strtotime('first day of this month')));

Link to comment
Share on other sites

Here's the code to generate a period.

 

$dates = array(
	date('Y-m-d', strtotime('+2 days', strtotime('last Friday', strtotime('first day of this month last month')))),
	date('Y-m-d', strtotime('+1 day', strtotime('last Friday', strtotime('first day of this month')))),
      );

print_r($dates);

 

Good luck!

Link to comment
Share on other sites

And here's the code to generate that table of yours, for testing purposes:

 

for ($i = 2; $i <= date('n'); $i++)
   echo date('M', mktime(0, 0, 0, $i - 1, 1)), ".\t", date('n/j', strtotime('+2 days', strtotime('last Friday', strtotime('first day of this month last month', mktime(0, 0, 0, $i, 1))))), '-', date('n/j', strtotime('+1 day', strtotime('last Friday', strtotime('first day of this month', mktime(0, 0, 0, $i, 1))))), '<br>';

Link to comment
Share on other sites

Cool. How reliable is strtotime?  How does this code look?  It seems to work well.  If you see something I should alter, please let me know.

$sunday=date('Y-m-d', strtotime('last Sunday', strtotime('last Saturday', strtotime('now'))));
$saturday=date('Y-m-d', strtotime('last Saturday', strtotime('now')));
$firstday=date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month'))));
$lastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month'))));

echo"$sunday<br />$saturday<hr />";
echo"$firstday<br />$lastday";

Link to comment
Share on other sites

Never use strtotime('now') it's completely redundant. strtotime always assumes "now" which in PHP is written as the return of function time().

 

$sunday = date('Y-m-d', strtotime('last Sunday', strtotime('last Saturday')));
$saturday = date('Y-m-d', strtotime('last Saturday'));
$firstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month'))));
$lastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month'))));

echo"$sunday<br />$saturday<hr />";
echo"$firstday<br />$lastday";

 

Not sure though those are the dates you wanted. See my previous code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.