Re: Brilliance requested - calculating a date next month [message #174471 is a reply to message #174453] |
Mon, 13 June 2011 10:46 |
bill
Messages: 310 Registered: October 2010
Karma:
|
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
|
|
|