A quick idea that can probably be perfected:
<?php
function calc_weekday_dates ($weekday, $year)
{
$weekday_options = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');
// Check argument $weekday
if( !in_array( strtolower($weekday), $weekday_options) )
{
return false;
}
// Check argument $year.
if( $year < 1900 || $year > 2050 )
{
return false;
}
$ini_date = mktime(0, 0, 0, 1, 1, $year); // Jan. 1
$end_date = mktime(0, 0, 0, 12, 31, $year); // Dec. 31
// The array to be returned.
$weekday_dates = array();
// There are 86400 seconds in a day.
for( $i = $ini_date; $i <= $end_date; $i += 86400 )
{
if( strtolower( date('D', $i) ) == strtolower($weekday) )
{
// Format the wanted dates.
$weekday_dates[] = date('y-m-d', $i);
}
}
return $weekday_dates;
}
$test = calc_weekday_dates ('Tue', 2004);
print '<pre>';
print_r($test);
print '</pre>';
?>
Seems to work. Now you can improve on it.
Cheers,
Erik Fleischer
On 13 Jan 2004 at 15:07, nabil wrote:
> Greetings;
>
> I need a help here ... I want to make function that return the dates of
> Tuesdays in a given year
>
> Example::
>
> Calc_Tuesdays (2004);
>
> //and it should return or echo
> 2004-01-06
> 2004-01-13
> 2004-01-20
> 2004-01-27
> 2004-02-03
> .
> .
> etc .... ofcourse it should echo 5 Tuesdays in March by example
> so it should depends on the year not only current day+7
>
> any help please !!
> Nabil
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php