Re: Brilliance requested - calculating a date next month [message #174458 is a reply to message #174450] |
Sun, 12 June 2011 20:59 |
Luuk
Messages: 329 Registered: September 2010
Karma:
|
Senior Member |
|
|
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
|
|
|