# Calculating week range from specific date - Any functions?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

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

## Re: Calculating week range from specific date - Any functions?

"Dave Smithz" <SPAM FREE WORLD> wrote in

this is really great:

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

## Re: Calculating week range from specific date - Any functions?

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.

## Re: Calculating week range from specific date - Any functions?

On Sat, 7 May 2005 11:22:51 +0100, "Dave Smithz" wrote:

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 /

## Re: Calculating week range from specific date - Any functions?

On Fri, 06 May 2005 17:44:26 +0100, Dave Smithz wrote:

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);

--
Optimiser Pty Ltd

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

## Re: Calculating week range from specific date - Any functions?

On Tue, 10 May 2005 16:09:46 +0800, Adam Harvey wrote:

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 /

## Re: Calculating week range from specific date - Any functions?

Ewoud Dronkert wrote:

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.
jstucklex@attglobal.net
==================