function dateToText($date) {
$monthOfYear = date("F Y", strtotime($date));
$daysInMonth = date("d", strtotime("last day of {$monthOfYear}"));
$dayOfMonth = date("d", strtotime($date));
$dayOfWeek = date("N", strtotime($date));
$occurance = 0;
$maxOccurance = 0;
foreach (range(1, $daysInMonth) as $day) {
if (date("N", strtotime("{$day} {$monthOfYear}")) == $dayOfWeek) {
$maxOccurance++;
if ($day <= $dayOfMonth) {
$occurance++;
}
}
}
$weekdayNumber = $occurance == $maxOccurance
? "last"
: match($occurance) {
1 => "first",
2 => "second",
3 => "third",
4 => "fourth"
}
;
$text = $weekdayNumber . " " . date("l \\o\\f F", strtotime($date));
return strtolower($text);
}