FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Brilliance requested - calculating a date next month
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Brilliance requested - calculating a date next month [message #174450] Sun, 12 June 2011 19:05 Go to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
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 [message #174453 is a reply to message #174450] Sun, 12 June 2011 19:46 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(bill)

> 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.

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 [message #174454 is a reply to message #174450] Sun, 12 June 2011 19:49 Go to previous messageGo to next message
Mathieu Maes is currently offline  Mathieu Maes
Messages: 5
Registered: May 2011
Karma: 0
Junior Member
On 12 jun, 21:05, bill <nob...@spamcop.net> wrote:
> 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

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==13)
{ // 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 [message #174457 is a reply to message #174454] Sun, 12 June 2011 20:43 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 6/12/2011 3:49 PM, Mathieu Maes wrote:
> On 12 jun, 21:05, bill<nob...@spamcop.net> wrote:
>> 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
>
> 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==13)
> { // December -> January next year
> $month = 1;
> $year++;
> }
>
> echo "Date in the future is " . $day . "-" . $month . "-" . $year;
> ?>
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 [message #174458 is a reply to message #174450] Sun, 12 June 2011 20:59 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 12-06-2011 21:05, bill wrote:
> 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

<?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 [message #174463 is a reply to message #174457] Mon, 13 June 2011 00:53 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 12 Jun 2011 16:43:29 -0400, bill wrote:

> On 6/12/2011 3:49 PM, Mathieu Maes wrote:
>> On 12 jun, 21:05, bill<nob...@spamcop.net> wrote:
>>> 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
>>
>> 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==13)
>> { // December -> January next year
>> $month = 1;
>> $year++;
>> }
>>
>> echo "Date in the future is " . $day . "-" . $month . "-" . $year; ?>
> 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.

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 [message #174466 is a reply to message #174450] Mon, 13 June 2011 02:11 Go to previous messageGo to next message
Chuck Anderson is currently offline  Chuck Anderson
Messages: 63
Registered: September 2010
Karma: 0
Member
bill wrote:
> 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


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 [message #174471 is a reply to message #174453] Mon, 13 June 2011 10:46 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 6/12/2011 3:46 PM, Michael Fesser wrote:
> .oO(bill)
>
>> 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.
>
> Not a solution, just some thoughts:
>
> You don't have to know which week you're in.

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 [message #174474 is a reply to message #174466] Mon, 13 June 2011 11:36 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 6/12/2011 10:11 PM, Chuck Anderson wrote:
> bill wrote:
>> 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
>
>
> 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.
>
couldn't be much simpler.
Thanks
Re: Brilliance requested - calculating a date next month [message #174476 is a reply to message #174463] Mon, 13 June 2011 13:00 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Mon, 13 Jun 2011 00:53:56 +0000, Denis McMahon wrote:

> On Sun, 12 Jun 2011 16:43:29 -0400, bill wrote:
>
>> On 6/12/2011 3:49 PM, Mathieu Maes wrote:
>>> On 12 jun, 21:05, bill<nob...@spamcop.net> wrote:
>>>> 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
>>>
>>> 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==13)
>>> { // December -> January next year
>>> $month = 1;
>>> $year++;
>>> }
>>>
>>> echo "Date in the future is " . $day . "-" . $month . "-" . $year; ?>
>> 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.
>
> I think I have something working here:
>
> http://www.sined.co.uk/tmp/nextmonth.2.php
>
Now: http://www.sined.co.uk/tmp/nextmonth.2.php
>
> The core function is:

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 [message #174478 is a reply to message #174450] Mon, 13 June 2011 19:46 Go to previous messageGo to next message
Mark Lloyd is currently offline  Mark Lloyd
Messages: 1
Registered: June 2011
Karma: 0
Junior Member
On Sun, 12 Jun 2011 15:05:48 -0400, bill <nobody(at)spamcop(dot)net> wrote:

> 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.

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 [message #174479 is a reply to message #174450] Mon, 13 June 2011 21:59 Go to previous message
BootNic is currently offline  BootNic
Messages: 10
Registered: November 2010
Karma: 0
Junior Member
On Sun, 12 Jun 2011 15:05:48 -0400
bill <nobody(at)spamcop(dot)net> wrote:

> 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.

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{2}, $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{2}, $year/", $NextMonth))?$NextMonth
:"no $when $day";
print "<div>".$NextMonth."</div>";

[snip]


--
BootNic Mon Jun 13, 2011 05:59 pm
When I was young, I was put in a school for retarded kids for two years before
they realized I actually had a hearing loss...and they called ME slow!
*Kathy Buckley*
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Strategic HR Summit 2011 (Jun 25, Mumbai)
Next Topic: MySQL's PASSWORD() function
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 12:28:38 GMT 2024

Total time taken to generate the page: 0.02663 seconds