2017-04-21 106 views
1

我試圖編寫PHP代碼,它將從特定月份開始計算週數。對我來說是九月和二月。例如01.09.2017期望的結果將是第一學期,第一週。而2017年4月9日將是第一學期,第二週。我發現類似的主題here here。但是他們的輸出結果是數組,我是否應該在這裏使用arreys呢?我想提一下,我幾乎沒有PHP語言expierence。從特定月份開始計算周

這是我想出迄今:

<?php 
$day = date("D"); 
$month = date("M"); 
if($month == 'Apr'||'Feb'||'Mar'||'May') { 
print "Semester-2,"; 
} 
else print ""; 
if($month == 'Sep'||'Oct'||'Nov'||'Dec') { 
print "Semester-1,"; 
} 
else print ""; 
if($month == 'Jan') { 
print "Exams"; 
} 
if($month == 'Jun') { 
print "Exams,"; 
} 
if($month == 'Jun') { 
print "Exams,"; 
} 
if($month == 'Jul'||'Aug') { 
print "Summer Break,"; 
} 
+0

'$ month =='Sep'||'Oct''不像您想象的那樣工作,它需要是'$ month =='Sep'|| $ month =='Oct''等,或者只是做'if(in_array($ month,array(「Apr」,「Feb」,「Mar」,「May」))'' – Qirel

回答

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; 
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"; 

?> 
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; 
} 

請注意,這可以重構爲更小,更多的語義部分。

相關問題