我写了一个小脚本并且不太明白为什么它不起作用。我想在下拉框中显示所有日历周(通常为每年52周)!我没有在互联网上找到类似的脚本,这就是我自己尝试的原因。有人能告诉我我做错了吗?
我只看到一个空的下拉框!如果有人可以帮助我会很棒。我花了一些时间来编写代码的这一部分。
编辑:
我的职能:
<?php
function weekDayToTime($week, $year, $dayOfWeek = 1) {
//dayOfWeek: 0=sunday, 1=monday, ..., 6=saturday
$dayOfWeekRef = date("w", mktime (0,0,0,1,4,$year));
if ($dayOfWeekRef == 0) $dayOfWeekRef = 7;
$resultTime = mktime(0,0,0,1,4,$year) + ((($week - 1) * 7 + ($dayOfWeek - $dayOfWeekRef)) * 86400);
$resultTime = cleanTime($resultTime); //Cleaning daylight saving time hours
return $resultTime;
};
function cleanTime($time) {
//This function strips all hours, minutes and seconds from time.
//For example useful of cleaning up DST hours from time
$cleanTime = mktime(0,0,0,date("m", $time),date("d", $time),date("Y", $time));
return $cleanTime;
}
function weeks($year)
{
return date("W",mktime(0,0,0,12,28,$year));
}
?>
<?php
$year = date("Y");
echo "<form method='GET' action='index.php'>"."<select name='w' onChange='this.form.submit()'>";
for($i=1;$i<=weeks($year);$i++)
{
$start = weekDayToTime($i, $year);
$end = cleanTime(604800 + $start);
if(weekDayToTime(date("W"), date("Y")) == $start)
{
$selected = "selected = 'selected' /";
}
else
{
$selected = '/';
}
echo "<option value='".$i."' $selected>KW ".$i.": ".strftime("%d. %B %Y", $start)." bis ".strftime("%d. %B %Y", $end)."</option>";
}
echo "</select>"."<input type='hidden' name='y' value='".$year."' />"."</form>";
?>
谢谢, 克里斯
答案 0 :(得分:1)
1 <?php
2 function weekDayToTime($week, $year, $dayOfWeek = 1) {
3 $dayOfWeekRef = date("w", mktime (0,0,0,1,4,$year));
4 if ($dayOfWeekRef == 0) $dayOfWeekRef = 7;
5 $resultTime = mktime(0,0,0,1,4,$year) + ((($week - 1) * 7 + ($dayOfWeek - $dayOfWeekRef)) * 86400);
6 $resultTime = cleanTime($resultTime); //Cleaning daylight saving time hours
7 return $resultTime;
8 };
9
10 function cleanTime($time) {
11 //This function strips all hours, minutes and seconds from time.
12 //For example useful of cleaning up DST hours from time
13 $cleanTime = mktime(0,0,0,date("m", $time),date("d", $time),date("Y", $time));
14 return $cleanTime;
15 }
16 function weeks($year)
17 {
18 return date("W",mktime(0,0,0,12,28,$year));
19 }
20
21 $year = date("Y");
22 echo('year is ' . var_export($year, true));
23 echo('weeks is ' . var_export(weeks($year), true));
24
25 echo "<form method='GET' action='index.php'>"."<select name='w' onChange='this.form.submit()'>";
26 for($i=1;$i<=weeks($year);$i++) {
27
28
29 $start = weekDayToTime($i, $year);
30 $end = cleanTime(604800 + $start);
31
32 if(weekDayToTime(date("W"), date("Y")) == $start) {
33 $selected = "selected = 'selected'";
34 } else {
35 $selected = '';
36 }
37
38 echo "<option value='".$i."' $selected>KW ".$i.": ".strftime("%d. %B %Y", $start)." bis ".strftime("%d. %B %Y", $end)."</option> \n";
39 }
40 echo "</select>"."<input type='hidden' name='y' value='".$year."' />"."</form> \n";
41
42 ?>
这是我的代码版本,似乎工作正常,我唯一改变的是从$ selected中删除斜杠,以便没有自动关闭选项标记。可能是吗?