How can I find out the current nth day of the month in php? Can I use the date function or time function to achieve this?
For example: 30th October 2014 is the 5th Thursday.
Thanks!
How can I find out the current nth day of the month in php? Can I use the date function or time function to achieve this?
For example: 30th October 2014 is the 5th Thursday.
Thanks!
This is an ordinal number algorithm.
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if (($number %100) >= 11 && ($number%100) <= 13)
$abbreviation = $number. 'th';
else
$abbreviation = $number. $ends[$number % 10];
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}
My complete solution to this question uses the addOrdinalNumberSuffix
function from the above answer from @ʰᵈ to format the output:
<?php
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}
echo addOrdinalNumberSuffix( ceil( date( 'j' ) / 7 ) ) . date( ' l' );
See it running here: http://codepad.org/sH7vWzcj