我希望能夠輸出時間表,其中包含表中包含的一系列日期的週數。舉例來說,假設我的日期是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.
}
?>
話說,10年12月9日是第1周無關的事實是,這是在12月的第一週滿2010? – John 2010-12-09 19:49:26
另外,1/3/11會在第5周? – John 2010-12-09 20:05:16
請澄清問題並添加一些輸出 – Gordon 2010-12-09 21:25:19