function calculateWeekdays($initial = '', $select = '') {
if(empty($select))$select = array();
if(empty($initial)) $initial = time();
$target = array('first' => 0,
'second'=> 1,
'third' => 2,
'fourth'=> 3,
'fith' => 4,
'last' => 5);
$s2long = array('sunday' => 'sun',
'monday' => 'mon',
'tuesday' => 'tue',
'wednesday' => 'wed',
'thursday' => 'thu',
'friday' => 'fri',
'saturday' => 'sat');
$pattern = array('0' => 'sun',
'1' => 'mon',
'2' => 'tue',
'3' => 'wed',
'4' => 'thu',
'5' => 'fri',
'6' => 'sat');
for($i=1;$i<=date('t', $initial);$i++) {
$temp = date('Y', $initial).'-'.date('m', $initial).'-'.$i;
$date = strtotime($temp);
for($d=0;$d<7;$d++) {
if(date('w', $date) == $d) {
$weekdays[$pattern[$d]][] = $date;
}
}
}
$select = explode(' ', $select);
if(count($select)<2) return array();
$key = $s2long[strtolower($select[1])];
$key2 = $target[$select[0]];
$return = ($key2 == 5) ? end($weekdays[$key]):$weekdays[$key][$key2];
if(empty($select)) return $weekdays;
return $return;
}