1

我目前正在从存储在 mysql 中的表单中获取日期时间,如下所示:

2013-03-18 09:00:00

但是,对于我正在构建的功能,我将一个事件插入到每周少于最后一周的日历中,除了开始日期已更改之外,插入基本上是复制的。

所以我得到这样的帖子:

if (isset($_POST['submit']))
{
    echo $start = $_POST['startDT'].':00';
}

这行得通。仅放置一周的功能也是如此。我坚持的是在数周内复制它。

所以我的函数看起来像这样:

if ($allYear = "yes")
{
    //Get Week in question - gives me the week number for that particular week
    $startW = getStartWeek($start);
        //It'll always be week 56 as the end week of the year
    $endW = 56;
        //while $i is less than or equal to 56, do it and add 1 to $i
    for($i = $startW; $i <= $endw; $i++)
    {
        $query = $mysqli->prepare("INSERT INTO `Events`(`Start`, `End`, `Group`, `Unit`, `Type`, `Room`, `Lecturer`) VALUES ('$start', '$end', '$group', '$unit', '$type', '$room', '$lecturer')");
        /*Here is where I'd add code to make the datetime, for example:
        * $start is 2013-03-18 09:00:00 and $end is 2013-03-18 10:00:00
        * I want $start in the next iteration to be 2013-03-25 09:00:00 and $end
        * to be 2013-03-25 10:00:00. I then want $start to be 2013-04-01 09:00:00
        * on the iteration after that. And so on.
        */
        $start += strtotime("+1 week");
        $end += strtotime("+1 week");
    }
}
4

2 回答 2

14

您应该真正使用有助于日期操作的 DateTime 类。http://www.php.net/manual/en/class.datetime.php

$date = DateTime::createFromFormat('Y-m-d H:i:s', '2013-03-18 09:00:00');
$date->modify('+1 week');
echo $date->format('Y-m-d H:i:s');
于 2013-04-07T05:06:24.257 回答
12

您可以通过这种方式添加一周的日期:

$date = "2013-07-04 08:10:25";
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('Y-M-d h:i:s', $date);
于 2013-04-07T05:08:34.520 回答