Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #2145
| From | Denis McMahon <denis.m.f.mcmahon@gmail.com> |
|---|---|
| Subject | Re: Brilliance requested - calculating a date next month |
| Newsgroups | comp.lang.php |
| References | <6NKdnWtJdY6Nk2jQnZ2dnUVZ_j-dnZ2d@cablespeedmi.com> <dac94570-4f8c-435a-bf38-2b933577f648@d1g2000yqm.googlegroups.com> <sOadneVSFvFouWjQnZ2dnUVZ_qmdnZ2d@cablespeedmi.com> <4df55fa4$0$2368$bed64819@gradwell.net> |
| Date | 2011-06-13 13:00 +0000 |
| Message-ID | <4df609f6$0$26157$bed64819@gradwell.net> (permalink) |
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
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web