# Brilliance requested - calculating a date next month

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

•  Subject
• Author
• Posted on
I am quietly banging my head on the wall.  In concept it seems so
simple:

I need to calculate the date of an appointment next month.
but:
it needs to be on the same day of the month,
eg: from the 2nd Tuesday of this month to the 2nd Tuesday of
next month.

obviously this will not work the 5th week of any month, so we can
limit it to the first 4 weeks of the "from" month (but this is
not the same as the first 28 days- for example January of this
year starts on a Saturday so the first "week" is only Jan 1. The
2nd "week" is Jan 2..8, the third "week" is Jan 9..15).

I was hoping that php would have a nifty dateTime function to do
this, but I can't find one: I checked out dateInterval and the
various date functions.

Right now I am so brain locked that I can't even figure out how
to calculate which week of the month is the first date.

Any suggestions, links, kind words would be appreciated.
bill

## Re: Brilliance requested - calculating a date next month

.oO(bill)

Not a solution, just some thoughts:

You don't have to know which week you're in. It should be enough to know
which Tuesday it is. Simply spoken you just have to count backwards in
steps of 7 until the result becomes zero or negative. With some thoughts
this should also be possible with some modulo operation or so.

Then, if you know that for example it's the 3. Tuesday, you can use
strtotime() to figure out the first Tuesday of the next month. Maybe it
even allows to directly get the 3rd Tuesday, but if not, you just add
3-1=2 weeks and you're done.

As said - just some quick thoughts.

HTH
Micha

## Re: Brilliance requested - calculating a date next month

On 6/12/2011 3:46 PM, Michael Fesser wrote:

Starting with this bit of Brilliance, it suddenly came to me that
the first Monday, first Tuesday, etc., all happen to occur in the
first 7 days of each month.  Likewise the second MOnday, etc. all
occur in days 8-14.
So, (as yet untested)
If the current appointment is on a Tuesday
\$Nmonth = next month // range 1-12
\$NYear  = next year
\$day    = current day of the month // limited to a range of 1-28
\$week =  floor(\$day/7); //range 0-4)

in the next month
\$start = strtotime(\$NYear. "-" . \$Nmonth . "-" . (\$week * 7) -1))
\$nextAppt = strtotime ("next Tuesday", \$start)

This will require some tweaking for the first two days of the
month, obviously

bill

## Re: Brilliance requested - calculating a date next month

Hi Bill,

This should do the trick:
<?php
// Get the day, month and year in seperate variables
\$day = date("d");
\$month = date("m");
\$year = date("Y");

if (\$day > 28)
{
// What happens if the current date is 29, 30 or 31? I will assume
the following:
\$day = 28;
}
// Increase the month by 1
\$month++;

if (\$month=3D=3D13)
{ // December -> January next year
\$month = 1;
\$year++;
}

echo "Date in the future is " . \$day . "-" . \$month . "-" . \$year;
?>

Kind regards,
Mathew

## Re: Brilliance requested - calculating a date next month

On 6/12/2011 3:49 PM, Mathieu Maes wrote:

Thanks Mathieu,
However it seems that this will give me the same date in the next
month, not the same day in the same week.

I think the other way does work though.
Let me play with it.

## Re: Brilliance requested - calculating a date next month

On Sun, 12 Jun 2011 16:43:29 -0400, bill wrote:

I think I have something working here:

http://www.sined.co.uk/tmp/nextmonth.php

The core function is:

<?php

// function next_month (timstamp)
// returns the nth xday in the next month
// eg if given the 3rd weds in june, gives timestamp of
// 3rd weds in july
// returns false if given the 5th xday of a month

function next_month(\$tstamp) {

\$dateinfo = getdate(\$tstamp);
\$hh = \$dateinfo['hours'];
\$mm = \$dateinfo['minutes'];
\$ss = \$dateinfo['seconds'];
\$mday = \$dateinfo['mday'];
\$mon = \$dateinfo['mon'];
\$year = \$dateinfo['year'];
\$w = \$dateinfo['wday'];

\$x = \$mday;
\$weeknum = 0;

while (\$x > 0) { // find week number in month
\$weeknum += 1;
\$x -= 7;
}

if (\$weeknum < 5) {

// start on the first of the next month
\$new_year = \$year;
\$new_mon = \$mon + 1;
\$new_mday = 1;
if (\$new_mon == 13) { // use next january
\$new_mon = 1;
\$new_year = \$new_year + 1;
}
\$new_time = mktime (\$hh, \$mm, \$ss, \$new_mon, \$new_mday,
\$new_year);

while (date("w", \$new_time) != \$w) { // find the first
// weekday \$w in next
// month
\$new_mday += 1;
\$new_time = mktime (\$hh, \$mm, \$ss, \$new_mon, \$new_mday,
\$new_year);
}

\$new_mday = \$new_mday + (\$weeknum - 1) * 7; // add weeks

\$new_time = mktime (\$hh, \$mm, \$ss, \$new_mon, \$new_mday,
\$new_year);

return(\$new_time);

}
return false;
}

?>

Rgds

Denis McMahon

## Re: Brilliance requested - calculating a date next month

On Mon, 13 Jun 2011 00:53:56 +0000, Denis McMahon wrote:

Now: http://www.sined.co.uk/tmp/nextmonth.2.php

now a few lines shorter and less complex

<?php

// function next_month (timstamp)
// returns the nth xday in the next month
// eg if given the 3rd weds in june, gives timestamp of
// 3rd weds in july
// returns false if given the 5th xday of a month

function next_month(\$tstamp) {

\$dateinfo = getdate(\$tstamp);

\$mday = \$dateinfo['mday'];
\$mon = \$dateinfo['mon'];
\$year = \$dateinfo['year'];
\$day = \$dateinfo['weekday'];

\$months = array (1 => "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

if (\$mday < 8) \$weeknum = 1;
elseif (\$mday < 15) \$weeknum = 2;
elseif (\$mday < 22) \$weeknum = 3;
elseif (\$mday < 29) \$weeknum = 4;
else \$weeknum = 5;

if (\$weeknum == 5) return false; // 5th Xxxday not guaranteed

\$new_year = \$year;
\$new_mon = \$mon + 1;
if (\$new_mon == 13) { // use next january
\$new_mon = 1;
\$new_year = \$new_year + 1;
}

\$tstr = sprintf("+%0d week %s %s %d", \$weeknum - 1, \$day,
\$months[\$new_mon], \$new_year);

\$new_time = strtotime(\$tstr);
return(\$new_time);
}

?>

Rgds

Denis McMahon

## Re: Brilliance requested - calculating a date next month

On 12-06-2011 21:05, bill wrote:

<?php
\$d = mktime(12, 0, 0, 6, 1, 2011);
print "Date: ".date("Y-m-d", \$d)."\n";
\$dow = date("N",\$d);
print "Day of week: \$dow\n\n";

if (\$dow>2) { \$diff = 7+(2-\$dow); } else { \$diff = 2-\$dow; }
\$d = \$d + (1*7+\$diff)*24*3600;

print "Date: ".date("Y-m-d", \$d)."\n";
\$dow = date("N D",\$d);
print "Day of week: \$dow\n\n";

\$d = \$d+(1-date("d",\$d)+date("t",\$d))*24*3600;
\$dow = date("N",\$d);
if (\$dow>2) { \$diff = 7+(2-\$dow); } else { \$diff = 2-\$dow; }
\$d = \$d + (1*7+\$diff)*24*3600;

print "Date: ".date("Y-m-d", \$d)."\n";
\$dow = date("N D",\$d);
print "Day of week: \$dow\n\n";

\$d = \$d+(1-date("d",\$d)+date("t",\$d))*24*3600;
\$dow = date("N",\$d);
if (\$dow>2) { \$diff = 7+(2-\$dow); } else { \$diff = 2-\$dow; }
\$d = \$d + (1*7+\$diff)*24*3600;

print "Date: ".date("Y-m-d", \$d)."\n";
\$dow = date("N D",\$d);
print "Day of week: \$dow\n\n";
?>

output:
Date: 2011-06-01
Day of week: 3

Date: 2011-06-14
Day of week: 2 Tue

Date: 2011-07-12
Day of week: 2 Tue

Date: 2011-08-09
Day of week: 2 Tue

--
Luuk

## Re: Brilliance requested - calculating a date next month

bill wrote:

The strtotime function will do this for you. When I needed to do the
same thing I found this note quite helpful.

http://www.php.net/manual/en/function.strtotime.php#94423

I ended up simply doing this (no plus and minus for instance, except the
special case = 5):

<?php
//****
example
//
\$instance = 3; // 0 - 5 (0 is the same as the first (1) instance;
// 5 is the "last" instance)
\$day_of_week = 'Monday'; // Sunday - Saturday
\$month = 'June';
\$year = 2011;
//
//****

echo date('Ymd', strtotime("\$instance \$day_of_week \$month \$year"));
?>

Note that the fifth instance (5) can be done. If instance is 5 (i.e.,
the last instance in the month) you can use -1 for instance of the day
of week of the *next* month.

<?php
if (\$instance == 5) // get -1 instance of day of week from next \$month
{
\$month = date('n', strtotime("+1 month \$month \$year"));
\$instance = -1;
}
?>

I'll leave it to you to handle December.

--
*****************************
Chuck Anderson • Boulder, CO
http://cycletourist.com
Turn Off, Tune Out, Drop In
*****************************

## Re: Brilliance requested - calculating a date next month

On 6/12/2011 10:11 PM, Chuck Anderson wrote:

couldn't be much simpler.
Thanks

## Re: Brilliance requested - calculating a date next month

I handle meetings which are always on the 2nd Thursday of the month. I
have a simple way to calculate the date of next month's meeting if I
know this months'. It should work the same for Tuesdays:

1. \$day_of_next_months_meeting = \$day_of_this_months_meeting

2. \$excess = \$days_in_current_month - 28

3. \$day_of_next_months_meeting -= \$excess

4. if (\$day_o f_next_months_meeting < 8) \$day_of_next_months_meeting
+=  7  //make sure its in the second week

For example, 2nd Tuesday in June is 14

Step 1: 14
Step 2: 2 (June has 2 more days than a whole number of weeks)
Step 3: 12
Step 4: 12 (in this case, July 12 is the 2nd Tuesday)

--
Mark Lloyd
http://notstupid.us

"If God wants us to do a thing, He should make his wishes sufficiently
clear. Sensible people will wait till He has done this before paying
much attention to Him." -- Samuel Butler

## Re: Brilliance requested - calculating a date next month

--Sig_/1lnm.G=I4.sWLQc4G2Q2L6J
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding

On Sun, 12 Jun 2011 15:05:48 -0400

would be neat if one could just say 'second tue of june 2011'

strtotime or DateTime

\$month = 'june';
\$year = 2011;

\$a = array('first', 'second', 'third', 'fourth', 'fifth');
\$d = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');

\$when = \$a[1];
\$day = \$d[2];

\$date = new DateTime("\$year-\$month-01");
\$month = \$date->format('F');
\$date->modify("\$when \$day of \$month 2011");
\$ThisMonth = \$date->format('l F d, Y');
\$ThisMonth = (preg_match("/\$month \d, \$year/", \$ThisMonth))?\$ThisMonth
:"no \$when \$day";
print "<div>".\$ThisMonth."</div>";
\$date->modify("+1 month");
\$month = \$date->format('F');
\$NextMonth = \$date->modify("\$when \$day of \$month 2011");
\$NextMonth = \$NextMonth->format('l F d, Y');
\$NextMonth = (preg_match("/\$month \d, \$year/", \$NextMonth))?\$NextMonth
:"no \$when \$day";
print "<div>".\$NextMonth."</div>";

[snip]

--=20
BootNic                                               Mon Jun 13, 2011 05:5=
9 pm
When I was young, I was put in a school for retarded kids for two years b=
efore
they realized I actually had a hearing loss...and they called ME slow!
*Kathy Buckley*

--Sig_/1lnm.G=I4.sWLQc4G2Q2L6J
Content-Type: application/pgp-signature; name=signature.asc
Content-Disposition: attachment; filename=signature.asc

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAk32iEcACgkQmo2774GZ7qnjRQCgx22uVpYW+B8qrKpO1ZnHQ796
JFgAnioA7i2EeDO1CbOcGGnj3O26DQbD
=W0X8
-----END PGP SIGNATURE-----

--Sig_/1lnm.G=I4.sWLQc4G2Q2L6J--