Reply to Re: Finding Nearest Friday

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация