-2

Need some logic here:

Need to get day of month date("d")

What I know:

$year = 2013;
$month = 10;
$week_nr_of_month = 3; // from 1 to 6 weeks in month
$day_of_week = 0; // Sunday date("w")

Thanks for logic

Result must be: 13 October

jmp
  • 2,456
  • 3
  • 30
  • 47

2 Answers2

0

strtotime may be of help. Can't test right now, but...

$ordinal = array("first","second","third","fourth","fifth");
$weekdays = array("monday","tuesday","wednesday","thursday","friday","saturday","sunday");
$timestamp = strtotime($year."-".$month." ".$ordinal[$week_nr_of_month]." ".$weekdays[$day_of_week]);
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

This was fun to figure out.

<?php

$year = 2013;
$month = 10;
$week_nr_of_month = 3; // from 1 to 6 weeks in month
$day_of_week = 0; // Sunday date("w")

$start = new DateTime();
$start->setDate($year, $month, '1');
$end = clone $start;
$end->modify('last day of this month');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);

foreach ($period as $date) {
    if ($date->format('w') == $day_of_week) {
        $wom = week_of_month($date->format('Y-m-d'));
        if ($week_nr_of_month == $wom) {
            echo $date->format('Y-m-d');
        }
    }
}

function week_of_month($date) {
    $dt = new DateTime($date);
    $bg = clone $dt;
    $bg->modify('first day of this month');
    $day_of_first = $bg->format('N');
    $day_of_month = $dt->format('j');
    return floor(($day_of_first + $day_of_month - 1) / 7) + 1;
}

See it in action

Used this answer for inspiration to get the week number for the date.

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496