-3

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!

  • In other words, you want to have a date and then convert it into the `nth` instance of the day of week in that month. What have you tried so far? – Ramsay Smith Oct 30 '14 at 13:28
  • I found on another post that I could do: $dw = date( "w", $timestamp); to find an integer representation of the current day. I'm currently wondering how to get the 'nth' day part. Do I have to find the start of the month and manually count up in 7s or is there a sleeker way? – Vishvanand Oct 30 '14 at 13:31
  • Personally I'd use DateTime objects and DatePeriods, but I'm sure it can be done with date or time functions (albeit probably requiring a lot more code) – Mark Baker Oct 30 '14 at 13:31
  • In the comments it looks like you're just trying to say that, for example, today is the 30th (vs the 31st). But in the question it seems like you want to say that today is the 5th Tuesday, which is very different - counting occurrences of a specific day of the week. Which part are you actually asking for help with? – Sam Hanley Oct 30 '14 at 14:11
  • Um, I needed to find both the current day in words and the nth day of the month it is. In my previous comment I was explaining my thought process to figure out the current day and use the date to find out the first day of the month and then count up in 7s to find the nth day. Regardless, my issue has been solved! Thanks for everyone's help! – Vishvanand Oct 30 '14 at 14:35

2 Answers2

0

This is an ordinal number algorithm.

StackOverflow

Live Preview

$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];

if-not-true-then-false

Live Preview

 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';
  }
Community
  • 1
  • 1
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

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

Community
  • 1
  • 1
wedi
  • 1,332
  • 1
  • 13
  • 28
  • What does your answer add besides just copying the code provided by the first answer? Just because you linked to the original answer doesn't mean you can just copy their code and re-answer it. – Sam Hanley Oct 30 '14 at 14:04
  • My answer completely answers the question which was to find "current nth day of the month in php". The cited answer only explained how to add the ordinal sumber suffix but no code how to actually compute the current nth day. I could have written "Use the above answer to format the desired result according to your wishes." but I opted for the copying solution as I wanted to provide completely working code answering the question. – wedi Oct 30 '14 at 14:10
  • Thank you for the snippet of code, I got my application of code to work to the requirement! – Vishvanand Oct 30 '14 at 14:37