1

I have the year, the week day, the week and the month, I'd like to return the day of the month.

I mean It returns me with dayweek, week, month and year:

$month = "September";
$year = "2013";
$dayWeek= "Friday";
$week = 2;

It returns the day of the month: it could be 1 or 2 or 3 or 4 or 5 etc.

Thanks!

  • Please provide expected output, as well as some code indicating you've tried to solve this problem yourself. – miken32 Nov 13 '19 at 22:11
  • The expected output would be the day of the month in number 1 or 2 or 3 or 4 or 5 or 6 etc etc @miken32 – Jesús Cova Nov 13 '19 at 22:14
  • What do you mean by `$week = 2`? If a month starts on a Wednesday, is `$week = 1` from that Wednesday until the next Wednesday? Or is it until the next monday/sunday? Also, what is the first day of the week? – n8jadams Nov 13 '19 at 22:34

2 Answers2

2

Try this

// your input
$month      = "September";
$year       = "2013";
$dayWeek    = "Friday";
$week       = 2;


// create a date object
$date = new DateTime();

// set to the first day of the specified year/month
$date->modify($year . '-' . $month . '-01');

// add $week -1 weeks to the date
$date->modify('+' . ($week - 1) . ' week');

// set to day of week
$date->modify($dayWeek);

// here's the day of month for you
echo $date->format('j');

returns 2013-09-13

Lee Salminen
  • 900
  • 8
  • 18
0

You cat try do it using nesbot/carbot package:

use Carbon\Carbon;

...

switch($week) {
    case 1:
        $weekNumber = 'first';
        break;
    case 2:
        $weekNumber = 'second';
        break;
    case 3:
        $weekNumber = 'third';
        break;
    case 4:
        $weekNumber = 'fourth';
        break;
    default:
        $weekNumber = 'fifth';
}

// Now $date will be a Carbon\Carbon instance and $date->day will return day of month
$date = Carbon::parse(sprintf("%s %s of %s %s", $weekNumber, $dayWeek, $month, $year));
igronus
  • 486
  • 4
  • 12