是否可以在下拉列表中显示从1到52周的日期?默认为当前周

时间:2017-05-24 11:24:49

标签: javascript jquery jquery-ui

  

是否可以创建带有日期的1到52周的下拉列表?默认为当前周。

1 个答案:

答案 0 :(得分:1)

请尝试此代码

<?php
function weekDayToTime($week, $year, $dayOfWeek = 1) {
  $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));
}   

$year = date("Y");
echo('year is ' . var_export($year, true));
echo('weeks is ' . var_export(weeks($year), true));

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>Week:".$i.": From ".strftime("%d. %B %Y", $start)." to ".strftime("%d. %B %Y", $end)."</option> \n";
}
echo "</select>"."<input type='hidden' name='y' value='".$year."' />"."</form> \n";

?>