2010-12-09 36 views
1

我希望能夠輸出時間表,其中包含表中包含的一系列日期的週數。舉例來說,假設我的日期是12/9/10(星期四),12/13/10(星期一),12/15/10(星期三)和12/21/10(星期二)在MySQL表中記錄。使用PHP計算基於日期的星期數

我想輸出的東西,計算基於這些日期的週數,因此這樣的:1

週刊:10年12月9日 第2周:10年12月13日,12/15/10 第3周:12/21/10

我知道如何獲得特定年份的星期數(就像今天我們在第49周),但由於我可以有任何日期範圍,我計算周,而不是一年的一週。

我可以簡單地將一年中的幾週轉換爲一個計數並依次顯示它們(如果日期從第49周開始並經過第52周,第49周= 1周,第50周= 2等),但是如果我日期跨越2年(如12/25/10至1/2/11),這是有問題的。

任何幫助將不勝感激!我不需要MySQL代碼 - 只需要日期字符串轉換。我一直在旋轉我的輪子!

更新:只是想我會分享終於解決這個問題的代碼。這不是我的最終解決方案,因爲數據仍需要進行按摩,但我得到了我想要的數據,現在我可以使用這些數據。感謝所有發佈答案的人。

<?php 
header("Content-type: text/html; charset=utf-8"); 
require_once('includes/connections/know_db.php'); 
?> 

<?php 

//First let's get all the years any given project will span... 
mysql_select_db($database_know_db, $know_db); 
$query_GetYears = sprintf("SELECT DISTINCT(YEAR(target_date)) as project_years FROM project_items WHERE projects_id = 136 AND target_date IS NOT NULL ORDER BY project_years ASC"); 
$GetYears = mysql_query($query_GetYears, $know_db) or die(mysql_error()); 

//A function allowing us to extract the week of the year from the next query, and then convert its value into an integer. 
function ConvertToWeek($target_date) { 
    $week = date('W', strtotime($target_date)); 
    $week_int = intval($week); 
    return $week_int; 
} 

//Now let's loop through our years, and get project item data (via MySQL) for each... 
while ($row_GetYears = mysql_fetch_assoc($GetYears)) { 
    echo $row_GetYears['project_years']."<br />"; 

    mysql_select_db($database_know_db, $know_db); 
    $query_GetItems = sprintf("SELECT DISTINCT(target_date) FROM project_items WHERE projects_id = 136 AND target_date IS NOT NULL AND YEAR(target_date) = '".$row_GetYears['project_years']."' ORDER BY target_date ASC"); 
    $GetItems = mysql_query($query_GetItems, $know_db) or die(mysql_error()); 

    //Loop through the results of our project items, convert them to week numbers from our function, then toss them into an array. 
    while ($row_GetItems = mysql_fetch_assoc($GetItems)) { 
     $weeks[] = ConvertToWeek($row_GetItems['target_date']); 
     //Array_unique essentially removes duplicate numbers... 
     $result = array_unique($weeks); 
    } 

    // get the first value in the $weeks array, then to be safe, convert its value to an integer. 
    $start_week = array_shift(array_values($weeks)); 
    $start_week_no = intval($start_week); 

    // get the last value in the $weeks array (will use this later to find differences in weeks between overlapping years). 
    $end_week = array_pop(array_values($weeks)); 

     echo 'Start week: '.$start_week_no."<br />"; 
     echo 'End week: '.$end_week."<br />"; 

    //Output our weeks for the purposes of display. 
    foreach ($result as $week_count) { 
     echo ltrim($week_count, "0").'<br />'; 
    } 

    /*Now let's find the weeks in the sequence where weeks are not represented (missing). 
    By doing this, we can get a number of for each week we don't have where datasets will be empty. 
    */ 

    // construct a new array:1,2....max(given array). 
    $result2 = range($start_week_no,max($result));              

    // use array_diff to get the missing weeks 
    $missing = array_diff($result2,$result); 

    //Output our missing weeks for the purposes of display. 
    foreach ($missing as $missing_weeks) { 
      echo $missing_weeks.' (missing)<br />'; 
    } 

    /* 
    Before we close our original while loop--the one that loops through each year, we need to unset our arrays so they are empty upon 
    each new loop. 
    */ 
    unset($weeks); 
    unset($result); 

//End our original while loop. 
} 
?> 
+1

話說,10年12月9日是第1周無關的事實是,這是在12月的第一週滿2010? – John 2010-12-09 19:49:26

+0

另外,1/3/11會在第5周? – John 2010-12-09 20:05:16

+0

請澄清問題並添加一些輸出 – Gordon 2010-12-09 21:25:19

回答

1

由於請求涉及複雜的東西,答案也會如此。

  • 排序日期ASC(顯然)
  • 運行每經過一個循環
    • 獲得一個變量年該日期:$year = date('Y',strtotime($date);)
    • 獲得一個變量一週該日期:$week = date('W',strtotime($date);)
    • 創建使用這些值的$ yearWeek值:$yearWeek = $year.$week;
    • Push that date into an arrayarray_push($weekArray,$yearWeek,$date);
  • 完成與循環,通過這一點,我想...
  • 採取這一陣列和排序它sort($weekArray);
  • 運行你的結果在一個新的循環,你知道的,就像一個foreach
    • 對於每一個獨特$年周,添加到您的$ weekCounter,比如$ weekCounter ++
    • 回聲出使用在$ weekCounter
計算變量的數組210

不知道這是否行得通,稍後可能會嘗試,但發佈這只是爲了讓你知道你可以做什麼。這不是一個完整的解決方案,但作爲一個起點,它可能是值得工作的。

1

從你的榜樣日期,它看起來像你正在做一個普通日曆星期,所以你沒有意思的一週爲「啓動」你的第一次約會。

不是一個PHP專家,但你可以這樣做:

  • 計算第一次約會的星期(根據您的任何一週的開始是 - 星期日,星期一,等等)。
  • 計算從第一個日期開始的所有較晚日期的差異,以天爲單位。
  • 將第一個日期的日期偏移添加到所有較晚的日期。
  • 將所有這些除以7,丟棄剩餘部分,並有你的答案。
2

以開始日期並從中獲取當前星期編號(如果您願意的話)。我可能會建議把它乘以一年或某事,以便知道年份重疊的時間。然後,採取所有繼任者,並做同樣的事情(周*年),並從中減去基數。

0

我不需要MySQL代碼 - 只是 日期字符串轉換

date('W', strtotime('12/21/10')) 

這將返回星期該日期。

0

你可以嘗試這樣的事:

SELECT (YEAR(date_field) * 100 + WEEK(date_field)) - (SELECT MIN(YEAR(date_field) * 100 + WEEK(date_field)) - 1 FROM table) AS week_number FROM table; 

此查詢創建從當年和週數的整數。年份乘以100爲本週數增加空間。這樣,無論年份如何,您都可以獲得每週的唯一號碼。例如。今天(第49周)的數字將爲201049.查詢從這個數字中減去最小數字以得到從0開始的星期數字。在子查詢末尾註意 - 1。它使得週數從1開始。

使用此查詢結構,您不必採取額外的步驟來解決項目從年底開始到結束的過程。

1

這是我想出的一個函數,用來查找任意兩個日期之間過去的星期數。

public function weeks_past(){ 
    $start_date = strtotime('01/07/2012'); 
    $end_date = strtotime('01/05/2013'); 

    $start_week = date('W', $start_date); 
    $end_week = date('W', $end_date); 

    $start_year = date('Y', $start_date); 
    $end_year = date('Y', $end_date); 
    $years = $end_year-$start_year; 

    if($years == 0){ 
     $weeks_past = $end_week-$start_week+1; 
    } 
    if($years == 1){ 
     $weeks_past = (52-$start_week+1)+$end_week; 
    } 
    if($years > 1){ 
     $weeks_past = (52-$start_week+1)+$end_week+($years*52); 
    } 

    return $weeks_past; 
} 

這將輸出53變星期過去,因爲01/07/2012

相關問題