Jump to content

Need Help with dates - Sun - Sat


vacek
Go to solution Solved by requinix,

Recommended Posts

Hi,

 

I'm in need of a bit of help with the following.

 

Right now, I'm using the following to get the date on Sunday and Saturday of the current week.

$startweek = date('m-d-Y',time()+( 0 - date('w'))*24*3600);
$endweek = date('m-d-Y',time()+( 6 - date('w'))*24*3600);

For the current week, this will return: 03-09-2014 & 03-15-2014

 

This works great for the current week. However I would like to pass it a variable ($weeknum) and have it calculate the dates of Sunday and Saturday of that particular week.

 

I know that this ( $date=date("W"); )  will return the current week number (although flawed for me as it is Monday -> Sunday I believe.)

 

I have tried the following, but the results are not what I expected. They are a week before the week I need.

$weeknum = date('W');

$startweek = date('m-d-Y',time()+( 0 - $weeknum)*24*3600);
$endweek = date('m-d-Y',time()+( 6 - $weeknum)*24*3600);

So 2 things I clearly need:

1.) A way to get the current week #, Sunday ->Saturday

2.) The dates of the current week, Sunday & Saturday .. when the $weeknum is passed to it.

 

Thanks !

Link to comment
Share on other sites

It depends on how you define weeks - particularly around the boundaries.

 

Consider December 31st, 2013 (a Tuesday) and January 1st, 2014 (a Wednesday). What week numbers do they get?

a) 2013-W52 and 2013-W52 because the week started in 2013? January's is a bit odd.

b) 2013-W52 and 2014-W01 because of their respective years? Neither week has seven days: three for 2013-W52 and four for 2014-W01.

c) 2014-W01 and 2014-W01 because the week range contained January 1st? December's is a bit odd. (This is how 'W' works.)

Link to comment
Share on other sites

Great points ...

 

As long as I can get 95% of the year right, I'll be good.  :tease-03:

 

Most important for me, is to get the dates in any particular week Sunday to Saturday ... (week number not as important, and I can just not use that at all)

 

I just need them to show up on the webpage automatically each week.

 

Thanks !

Link to comment
Share on other sites

  • Solution

As long as I can get 95% of the year right, I'll be good.  :tease-03:

Please, no. That mentality causes so many problems with software and I, for one, find it incredibly frustrating when people use it.

 

Most important for me, is to get the dates in any particular week Sunday to Saturday ... (week number not as important, and I can just not use that at all)

That's actually what I thought, but you kept talking about the week number.

 

So you've got a date and you want to know the previous Sunday and the next Saturday? The very first bit of code you posted was correct-ish - just had to make time() a variable.

$date = strtotime("2014-01-01"); // for example

// break out the parts of the date
list($y, $m, $d, $w) = explode("-", date("Y-m-d-w", $date));

// sunday is w days ago
$sunday = mktime(0, 0, 0, $m, $d - $w, $y);

// saturday is (6 - w) days in the future
$saturday = mktime(0, 0, 0, $m, $d + 6 - $w, $y);
Adding seconds, like you had with 24*3600, is imprecise because not all days have 86400 seconds. Plus daylight savings can give you unexpected results.
Link to comment
Share on other sites

So i took a swag at implementing this .. something is not right:

$date = date('d.m.Y',strtotime("2014-01-01")); // for example

// break out the parts of the date
list($y, $m, $d, $w) = explode("-", date("Y-m-d-w", $date));

// sunday is w days ago
$sunday = date('d.m.Y', mktime(0, 0, 0, $m, $d - $w, $y));

// saturday is (6 - w) days in the future
$saturday = date('d.m.Y',mktime(0, 0, 0, $m, $d + 6 - $w, $y));

My results using the code as above are:

 

01.01.2014
28.12.1969
03.01.1970

 

Thoughts ?

Link to comment
Share on other sites

sorry, was self inflicted :)

 

This code works:

$date = strtotime("2014-01-01"); // for example

// break out the parts of the date
list($y, $m, $d, $w) = explode("-", date("Y-m-d-w", $date));

// sunday is w days ago
$sunday = mktime(0, 0, 0, $m, $d - $w, $y);

// saturday is (6 - w) days in the future
$saturday = mktime(0, 0, 0, $m, $d + 6 - $w, $y);


$date1 = date('d.m.Y', $date);
$sunday1 = date('d.m.Y', $sunday);
$saturday1 = date('d.m.Y', $saturday);


echo $date1.'<BR>';
echo $sunday1.'<BR>';
echo $saturday1;

01.01.2014
29.12.2013
04.01.2014

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.