如何获得一天的月份

时间:2016-09-24 09:33:05

标签: php date

如果我输入日期05-09-2016,它怎么能告诉我它的第一个星期一。就像我把08-09-2016比它显示第二个星期四一样

如果我计算的是周而不是5日它显示第2周这是正确的,但星期一是第一周。所以我怎么能像

那样实现它
$getDayCount=$getDate(05-09-2016);

Which will show me 1 Monday

我从这个功能获得周

public function weekOfMonth($date) {

        // estract date parts
        list($y, $m, $d) = explode('-', date('Y-m-d', strtotime($date)));

        // current week, min 1
        $w = 1;

        // for each day since the start of the month
        for ($i = 1; $i <= $d; ++$i) {
            // if that day was a sunday and is not the first day of month
            if ($i > 1 && date('w', strtotime("$y-$m-$i")) == 0) {
                // increment current week
                ++$w;
            }
        }

        // now return
        return $w;
    }

1 个答案:

答案 0 :(得分:0)

在完成它之后,我为它创建了一些代码。它很大,但它的工作

$date=  strtotime($date);
$year=  date('Y',$date);
$month=date('m',$date);
$newdate=  strtotime("1-$month-$year");
$firstday=date('l',$newdate);    
$firstDayNo=  getDay($firstday);
$daySpell=date('l',$date);
$chooseDayNo=  getDay($daySpell);
$day = $date;
$weekNum = date('W', $day) - date('W', strtotime(date('Y-m-01', $day))) +1;

if($chooseDayNo < $firstDayNo){
    $week=  getWeek($weekNum -1);
}
else
{
    $week=  getWeek($weekNum);
}

function getDay($day){
switch ($day) {
    case "Monday":return 1;
        break;
    case "Tuesday": return 2;
        break;
    case "Wednesday": return 3;
        break;
    case "Thursday": return 4;
        break;
    case "Friday": return 5;
        break;
    case "Saturday": return 6;
        break;
    case "Sunday": return 7;
        break;
}
}

function getWeek($week){
    switch ($week) {
        case 1:return "First";
            break;
        case 2: return "Second";
            break;
        case 3: return "Third";
            break;
        case 4: return "Fourth";
            break;
        case 5: return "Fifth";
            break;
        case 6: return "Sixth";
            break;
    }
}