【问题标题】:I need buttons that adds a week to date and one to decrease我需要一个按钮来增加一个星期的日期和一个减少一个星期
【发布时间】:2014-08-26 17:55:37
【问题描述】:

此代码显示周数

<?php 
    $week_number = date("W");
    echo "Week " . $week_number; 
?>

这段代码显示了那一周的日期

<?php
$week_number = date("W");
$year = date("Y");
for($day=1; $day<=7; $day++){
    echo "<td>";
    echo date('d M', strtotime($year."W".$week_number.$day))."\n";
    echo "</td>";
}

它是一周视图日历,我需要的是下一个和上一个按钮,可以跳到下周并返回上一个星期。请帮助我找不到使用按钮增加一周或减少一周的解决方案。

【问题讨论】:

    标签: php date button calendar add


    【解决方案1】:
    $last_week = (new DateTime('last week'))->format('d M');
    $next_week = (new DateTime('next week'))->format('d M');
    

    Demo

    如果你想从不同的日期开始,代码看起来不会有太大的不同:

    $date      = $_GET['date']; // Sample value: 2014-08-19
    $last_week = (new DateTime($date)->modify('last week'))->format('d M');
    $next_week = (new DateTime($date)->modify('next week'))->format('d M');
    

    【讨论】:

    • 如何把它变成一个按钮?
    • 只需使用&lt;button&gt; HTML 标签。现在,如果您想知道按下该按钮时如何使事情发生,那么您的问题就太宽泛了...
    • 这个按钮是我需要的,它需要将 $week_number 加或减一,以便 for 循环也能得到它
    • 只需使用此代码将日期传递给页面,并将日期作为参数传递给 DateTime()。这仍然会以这种方式工作。
    • 你能告诉我这是怎么做的吗?我不明白按钮部分在代码中是如何工作的。
    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多