在php中添加7天到datetime(而不仅仅是日期)

时间:2013-04-07 05:02:33

标签: php mysql datetime

我目前正在从存储在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");
    }
}

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分:10)

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

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