By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,407 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,407 IT Pros & Developers. It's quick & easy.

Calculating week range from specific date - Any functions?

P: n/a
Help,

I am sure this has been asked before but it is difficult to search for.
Given a date, is there a function or a already written script out there that
will allow my to get the previous Monday before the date given?

This is so that given the current date, I can show all events that are
happening for that week. I also will want to do the same for the month. I
know I could go about writing algorithms to calculate this, but I am sure
this is something that has been done numerous times before.

Also is there anywhere out there built an easy to plug in calendar view
function that rather then my user manually entering the dates they want to
select, can just select a particular date?

Kind regards

Dave
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Dave Smithz" <SPAM FREE WORLD> wrote in
news:42********@news1.homechoice.co.uk:
Help,

I am sure this has been asked before but it is difficult to search
for. Given a date, is there a function or a already written script out
there that will allow my to get the previous Monday before the date
given?

This is so that given the current date, I can show all events that are
happening for that week. I also will want to do the same for the
month. I know I could go about writing algorithms to calculate this,
but I am sure this is something that has been done numerous times
before.


this is really great:

http://www.phpclasses.org/browse/package/943.html
Jul 17 '05 #2

P: n/a

"Good Man" <he***@letsgo.com> wrote in message
news:Xn************************@216.196.97.131...
this is really great:

http://www.phpclasses.org/browse/package/943.html


Hmmm, did not immediately love it. Spent quite a lot of time getting it and
reading about it, and to be honest it is still quite long winded to achieve
the answer to a question like

What is the date of the Monday before the date I have given you. I still had
to write quite a lot of logic around the available functions to achieve
this. Maybe I'm using it wrong or missed something.

Looks useful if I want to work out roman numerals or moon phase etc.

Jul 17 '05 #3

P: n/a
On Sat, 7 May 2005 11:22:51 +0100, "Dave Smithz" wrote:
http://www.phpclasses.org/browse/package/943.html


Looks useful if I want to work out roman numerals or moon phase etc.


Haha!

To answer your question: no there is no single function to get the
Monday before given date. Try:

$mydate = mktime( 12, 0, 0, $month, $day, $year );
//or: $mydate = strtotime( 'some date string' );
//or get it from mysql_query( 'SELECT UNIXDATE(datefield) FROM table' )

//get weekday with monday==0, sunday==6
$weekday = ((int)date( 'w', $mydate ) + 6 ) % 7;

//get first monday on or before given date
$prevmonday = $mydate - $weekday * 24 * 3600;

//format date
echo 'Monday of this week: '.date( 'l j F Y', $prevmonday );
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4

P: n/a
On Fri, 06 May 2005 17:44:26 +0100, Dave Smithz wrote:
I am sure this has been asked before but it is difficult to search for.
Given a date, is there a function or a already written script out there that
will allow my to get the previous Monday before the date given?


If $ts is a UNIX timestamp for the date you want to use, the following
should put the right timestamp into $monday:

$monday = strtotime('last monday', $ts);

Adam

--
Adam Harvey
Optimiser Pty Ltd

To e-mail: don't make an example out of me!

Jul 17 '05 #5

P: n/a
On Tue, 10 May 2005 16:09:46 +0800, Adam Harvey wrote:
$monday = strtotime('last monday', $ts);


Except that for Mondays, it returns the previous Monday.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #6

P: n/a
Ewoud Dronkert wrote:
On Tue, 10 May 2005 16:09:46 +0800, Adam Harvey wrote:
$monday = strtotime('last monday', $ts);

Except that for Mondays, it returns the previous Monday.

Just move ahead a day...

$monday = strtotime('last monday', $ts+24*60*60);
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.