|
Posted by Steve on 10/03/06 15:00
| Code does not work for the following, any ideas why?
|
| print ( "Use date 2006-09-30");
| $prior_friday_calc = days_to_Friday(2006, 09, 30);
| echo "$prior_friday_calc<BR><BR>\n";
|
| function days_to_Friday($fyear, $fmonth, $fday)
| {
| $timestamp = mktime(0,0,0,$fmonth,$fday,$fyear);
| $lastFriday = date("Y-m-d",strtotime("last Friday",$timestamp));
| echo " $lastFriday<BR>\n";
| }
i don't know why specifically, but having worked with php dates before that
there would be issues finding the NEXT friday. your example above finds
2006-09-01 just fine! what it WON'T find is the NEXT friday which is
2006-09-08. it will find 2006-09-15 instead...which imho is wrong! anyway,
try this:
<pre>
<?
$date = '2006-09-03';
echo 'Initial Date: ' . $date . "\r\n";
$date = strtotime($date);
echo 'NEXT Friday: ' . date('Y-m-d', findFriday($date, 'next')) . "\r\n";
echo 'LAST Friday: ' . date('Y-m-d', findFriday($date, 'last')) . "\r\n";
function findFriday($date, $direction = 'next')
{
$friday = strtotime('last friday', $date);
if ($direction == 'last'){ return $friday; }
return strtotime('+7 days', $friday);
}
?>
</pre>
you can find the NEAREST friday (your op subject) by subtracting the
returned findFriday dates from the initial date...whichever is smaller is
the nearest friday. of course you'll need make sure you either abs the value
of the substraction or make sure you subtract using, or subtract from, the
initial date appropriately.
hth
Navigation:
[Reply to this message]
|