我正在尝试编写一个脚本,该脚本将在一个表格中显示一周的日期,如果单击一个按钮,则会提前一周。我已经设法让它一直工作,直到它到达年底,然后日期都出错了。他就是我到目前为止......
<?
if(isset($_POST['add_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '+1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else{
$week_number = date("W");
$year = date("Y");
}
if($week_number < 10){
$week_number = "0".$week_number;
}
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
echo $week.' '.$new_week.' '.$week_number;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" />
<input ype="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
有些值已被回复,所以我可以检查传递的值是否正确,我知道我可能已经采取了我不需要的额外步骤,但我对此很新,并希望制作当我开始工作时,代码更容易被删除。正如我所说的那样,添加按钮会有效,直到它迎来新的一年。
由于
好的,取得了一些进步,一直运行到2012年然后它再次运行到2012年,而不是从2013年开始
<?
if(isset($_POST['add_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '+1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else if(isset($_POST['back_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '-1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else{
$week_number = date("W");
$year = date("Y");
}
/*if($week_number < 10){
$week_number = "0".$week_number;
}*/
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
echo $week.' '.$new_week.' '.$week_number;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" /><input type="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
答案 0 :(得分:3)
在我看来,您将更好地根据unix时间戳值进行所有计算,然后根据输出的需要转换为字符串。这样你不必处理周数问题(即第0周),你不仅限于星期一是每周的第一天(这是date("W")
中计算的基础),并且你不需要做一堆黑客来寻找边缘条件。
因此假设$_POST['last_week']
符合您的d-m-Y格式:
if(isset($_POST['add_week'])){
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
$display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;
}
$week_start = date('d-m-Y', $display_week_ts);
对于循环显示一周的部分,您可以使用以下内容:
for ($i = 0; $i < 7; $i++) {
$current_day_ts = $display_week_ts + ($i * 3600 *24);
echo date('d-m-Y', $current_day_ts);
}