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
Return to the default flat view Create a new topic Submit Reply
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 previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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: Thu Nov 28 07:18:25 GMT 2024

Total time taken to generate the page: 0.17703 seconds