2

我正在尝试创建一个允许用户前进或后退一周的周历。到目前为止,我有这个...

<?
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);
    $week_number = date("W", strtotime( $display_week_ts));
    $year = date("Y", strtotime( $display_week_ts));

echo $week_start.' '.$week_number.' '.$year;
?>

<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>

后退和前进按钮工作得很好,代表一周中第一个日期的 $week_start 变量前进并按原样返回,但无论显示的日期如何 $week_number 和 $year 显示为 01 和 1970 或 36 和 1600 .

我知道这一定与我试图从 $display_week_ts 中提取它们的方式有关,但我不知道是什么

4

2 回答 2

3

以下看起来格格不入:

$week_start = date('d-m-Y', $display_week_ts);
$week_number = date("W", strtotime( $display_week_ts));
$year = date("Y", strtotime( $display_week_ts));

查看您$display_week_ts在第一个语句中的使用方式,但对于其他(和类似的)语句,您将该时间戳包装在对strtotime()返回的调用中false

最好只删除strtotime()并按原样使用变量:

$week_number = date("W", $display_week_ts);
$year = date("Y", $display_week_ts);
于 2012-09-04T16:28:25.273 回答
0

好的,修好了,我需要从 $week_start 获取周数和年份

于 2012-09-04T16:31:06.743 回答