Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #2116 > unrolled thread

Brilliance requested - calculating a date next month

Started bybill <nobody@spamcop.net>
First post2011-06-12 15:05 -0400
Last post2011-06-13 17:59 -0400
Articles 12 — 8 participants

Back to article view | Back to comp.lang.php


Contents

  Brilliance requested - calculating a date next month bill <nobody@spamcop.net> - 2011-06-12 15:05 -0400
    Re: Brilliance requested - calculating a date next month Michael Fesser <netizen@gmx.de> - 2011-06-12 21:46 +0200
      Re: Brilliance requested - calculating a date next month bill <nobody@spamcop.net> - 2011-06-13 06:46 -0400
    Re: Brilliance requested - calculating a date next month Mathieu Maes <mathieu@webberig.be> - 2011-06-12 12:49 -0700
      Re: Brilliance requested - calculating a date next month bill <nobody@spamcop.net> - 2011-06-12 16:43 -0400
        Re: Brilliance requested - calculating a date next month Denis McMahon <denis.m.f.mcmahon@gmail.com> - 2011-06-13 00:53 +0000
          Re: Brilliance requested - calculating a date next month Denis McMahon <denis.m.f.mcmahon@gmail.com> - 2011-06-13 13:00 +0000
    Re: Brilliance requested - calculating a date next month Luuk <Luuk@invalid.lan> - 2011-06-12 22:59 +0200
    Re: Brilliance requested - calculating a date next month Chuck Anderson <cycletourist@invalid.invalid> - 2011-06-12 20:11 -0600
      Re: Brilliance requested - calculating a date next month bill <nobody@spamcop.net> - 2011-06-13 07:36 -0400
    Re: Brilliance requested - calculating a date next month Mark Lloyd <mlloyd@notroachstupid.usant.invalid> - 2011-06-13 14:46 -0500
    Re: Brilliance requested - calculating a date next month BootNic <bootnic.bounce@gmail.com> - 2011-06-13 17:59 -0400

#2116 — Brilliance requested - calculating a date next month

Frombill <nobody@spamcop.net>
Date2011-06-12 15:05 -0400
SubjectBrilliance requested - calculating a date next month
Message-ID<6NKdnWtJdY6Nk2jQnZ2dnUVZ_j-dnZ2d@cablespeedmi.com>
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

[toc] | [next] | [standalone]


#2119

FromMichael Fesser <netizen@gmx.de>
Date2011-06-12 21:46 +0200
Message-ID<pe5av698fh7lm6np5qva8p26tflon3t5lt@mfesser.de>
In reply to#2116
.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

[toc] | [prev] | [next] | [standalone]


#2137

Frombill <nobody@spamcop.net>
Date2011-06-13 06:46 -0400
Message-ID<0qidnV4IzPgVd2jQnZ2dnUVZ_oednZ2d@cablespeedmi.com>
In reply to#2119
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

[toc] | [prev] | [next] | [standalone]


#2120

FromMathieu Maes <mathieu@webberig.be>
Date2011-06-12 12:49 -0700
Message-ID<dac94570-4f8c-435a-bf38-2b933577f648@d1g2000yqm.googlegroups.com>
In reply to#2116
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

[toc] | [prev] | [next] | [standalone]


#2123

Frombill <nobody@spamcop.net>
Date2011-06-12 16:43 -0400
Message-ID<sOadneVSFvFouWjQnZ2dnUVZ_qmdnZ2d@cablespeedmi.com>
In reply to#2120
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.

[toc] | [prev] | [next] | [standalone]


#2129

FromDenis McMahon <denis.m.f.mcmahon@gmail.com>
Date2011-06-13 00:53 +0000
Message-ID<4df55fa4$0$2368$bed64819@gradwell.net>
In reply to#2123
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

[toc] | [prev] | [next] | [standalone]


#2145

FromDenis McMahon <denis.m.f.mcmahon@gmail.com>
Date2011-06-13 13:00 +0000
Message-ID<4df609f6$0$26157$bed64819@gradwell.net>
In reply to#2129
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

[toc] | [prev] | [next] | [standalone]


#2124

FromLuuk <Luuk@invalid.lan>
Date2011-06-12 22:59 +0200
Message-ID<4df528cd$0$49041$e4fe514c@news.xs4all.nl>
In reply to#2116
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

[toc] | [prev] | [next] | [standalone]


#2132

FromChuck Anderson <cycletourist@invalid.invalid>
Date2011-06-12 20:11 -0600
Message-ID<it3rk1$dm0$1@dont-email.me>
In reply to#2116
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
*****************************

[toc] | [prev] | [next] | [standalone]


#2142

Frombill <nobody@spamcop.net>
Date2011-06-13 07:36 -0400
Message-ID<KNadnaxqlZTCa2jQnZ2dnUVZ_tmdnZ2d@cablespeedmi.com>
In reply to#2132
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

[toc] | [prev] | [next] | [standalone]


#2147

FromMark Lloyd <mlloyd@notroachstupid.usant.invalid>
Date2011-06-13 14:46 -0500
Message-ID<1bpcv61varurd3pqslfvqarchm9hh27074@4ax.com>
In reply to#2116
On Sun, 12 Jun 2011 15:05:48 -0400, bill <nobody@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.

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

[toc] | [prev] | [next] | [standalone]


#2148

FromBootNic <bootnic.bounce@gmail.com>
Date2011-06-13 17:59 -0400
Message-ID<20110613175934.082a4094@bootnic.eternal-september.org>
In reply to#2116

[Multipart message — attachments visible in raw view] — view raw

On Sun, 12 Jun 2011 15:05:48 -0400
bill <nobody@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. 

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*

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web