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


Groups > comp.lang.php > #2132

Re: Brilliance requested - calculating a date next month

From Chuck Anderson <cycletourist@invalid.invalid>
Newsgroups comp.lang.php
Subject Re: Brilliance requested - calculating a date next month
Date 2011-06-12 20:11 -0600
Organization cycletourist.com
Message-ID <it3rk1$dm0$1@dont-email.me> (permalink)
References <6NKdnWtJdY6Nk2jQnZ2dnUVZ_j-dnZ2d@cablespeedmi.com>

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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