从特定月份开始计算周数

时间:2017-04-21 18:54:56

标签: php

我正在尝试编写PHP代码,这些代码将从特定月份开始计算数周。对我来说是9月和2月。例如,01.09.2017的期望结果将是第一学期,第1周。而对于04.09.2017将是第一学期,第二周。我发现了类似的主题here here 。但是他们的输出结果是数组,我也应该在这里使用arreys吗?我想提一下,我几乎没有使用php语言进行expierence。

这是我到目前为止所提出的:

object-position: center center;

3 个答案:

答案 0 :(得分:1)

你可以这样做:

$month = date('n'); // Month number 1-12

if ($month >= 9 && $month <=12) {
    $period = 'Semester-1';
    $startWeek = date('W', strtotime(date('Y') . '-09-01'));
} elseif ($month >= 2 && $month <=5) {
    $period = 'Semester-2';
    $startWeek = date('W', strtotime(date('Y') . '-02-01'));
} elseif ($month == 1) {
    $period = 'Exams';
    $startWeek = date('W', strtotime(date('Y') . '-01-01'));
} elseif ($month == 6) {
    $period = 'Exams';
    $startWeek = date('W', strtotime(date('Y') . '-06-01'));
} elseif ($month == 7 || $month == 8) {
    $period = 'Summer break';
    $startWeek = date('W', strtotime(date('Y') . '-07-01'));
}

$currentWeek = date('W') - $startWeek + 1;

echo $period . ' ' . 'Week-' . $currentWeek;

答案 1 :(得分:0)

在@Qirel评论之后,我想到了这样的事情,希望这会有所帮助:

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);

$month = date("M"); /* current month */

if (in_array($month, array("Sep", "Oct", "Nov", "Dec"))) { $myperiod = "Semester #1"; }

if (in_array($month, array("Feb", "Mar", "Apr", "May"))) { $myperiod = "Semester #2"; }

if($month == 'Jan') { $myperiod = "Mid-Exams #1"; }

if($month == 'Jun') { $myperiod = "Final-Exams #2"; }

if( ($month == 'Jul') || ($month == 'Aug') ) { $myperiod = "Summer break"; }

$today = date("Y-m-d"); /* or use your date from user data */
$date = new DateTime($today);
$week = $date->format("W"); /* use of PHP function 'date' to get week # */

$currentweek = "$week";

echo "[ Week # $week ]";

echo"You currently are in : $myperiod - $currentweek";

?>

答案 2 :(得分:0)

这应该可以满足您的需求:

$semesters = array(
    'Sep' => 'Semester-1',
    'Oct' => 'Semester-1',
    'Nov' => 'Semester-1',
    'Dec' => 'Semester-1',
    'Jan' => 'Exams',
    'Feb' => 'Semester-2',
    'Mar' => 'Semester-2',
    'Apr' => 'Semester-2',
    'May' => 'Semester-2',
    'Jun' => 'Exams',
    'Jul' => 'Summer Break',
    'Aug' => 'Summer Break',
);

switch ($semesters[date('M')]) {
    case 'Semester-1':
        $sep1st = strtotime('2017-09-01');
        $week1 = date('W', $sep1st);

        $currentWeek = date('W');

        echo 'Semester-1, Week-', $currentWeek - $week1 + 1; // +1 because the count starts at 1.

        break;

    case 'Semester-2':
        $feb1st = strtotime('2018-02-01');
        $week1 = date('W', $feb1st);

        $currentWeek = date('W');

        echo 'Semester-2, Week-', $currentWeek - $week1 + 1; // +1 because the count starts at 1.

        break;

    default:
        echo $semesters[date('M')];

        break;
}

请注意,这可以重构为更小,更语义的部分。