我希望能够输出一个包含表格中包含的一系列日期周数的时间表。例如,假设我的日期为12/9/10(星期四),12/13/10(星期一),12/15/10(星期三)和12/21/10(星期二),每个在MySQL表中的记录中。
我想要输出的内容是根据这些日期计算周数,例如:
第1周:2010年9月12日 第2周:10/12/13/10,12/15/10 第3周:12/21/10
我知道如何获得给定年份的周数(所以就像今天我们在第49周),但由于我可以有任何日期范围,我计算周数,而不是一年中的一周。< / p>
我可以简单地将一年中的几周转换为计数并按顺序显示它们(如果日期从第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.
}
?>
答案 0 :(得分:2)
获取开始日期并从中获取当前周数(如果您愿意,则为基数)。我可能会建议将它乘以年份或其他东西,以便您知道年份何时重叠。然后,取所有后继者并做同样的事情(周*年)并从中减去基数。
答案 1 :(得分:1)
从您的示例日期开始,您似乎正在进行常规日历周,因此您并不意味着在第一次约会时“开始”这一周。
不是PHP专家,但你可以这样做:
答案 2 :(得分:1)
由于请求涉及复杂的内容,答案也是如此。
$year = date('Y',strtotime($date);)
$week = date('W',strtotime($date);)
$yearWeek = $year.$week;
array_push($weekArray,$yearWeek,$date);
sort($weekArray);
不知道这是否有效,可能会在以后进行实验,但发布此信息只是为了让您了解自己可以做些什么。这不是一个完整的解决方案,但作为起点可能值得一试。
答案 3 :(得分: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;
}
这将从2012年7月1日开始输出53周
答案 4 :(得分:0)
我不需要MySQL代码 - 只需要 日期字符串转换
date('W', strtotime('12/21/10'))
这将返回该日期的一周。
答案 5 :(得分: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开始。
使用此查询结构,您无需采取额外步骤来解决从年末开始到结束时开始的项目。