使用php获得接下来5天的约会

时间:2013-07-22 14:48:02

标签: php datetime

我正在尝试编写一个脚本来拉取接下来7天的日期,并将它们放入每个日期的div中:

    echo '<div class="dateboxcontainer">';
    for($i=0; $i<=6; $i++){
        echo '<div class="datebox"><div class="topdate">'.strtoupper(date("D d", mktime(0, 0, 0, 0, date("d")+$i, 0))."\n").
        '</div><div class="bottomdate">An appointment for the day</div></div>';
    }  
    echo '</div>';

enter image description here

我现在正试图从两个字段'datedroppingoff'和'datepickingup'中提取我的数据库中的数据,其格式如下:'2013-07-10 14:29:28'。

我有点陷入困境,因为我不知道要写什么查询将每天的约会放入每天“某些信息”当前所在的div。

我猜它会像

Select * FROM jobdetails WHERE datedroppingoff OR datepickingup = WHATEVER DAY IS BEING ECHO'D OUT

但我不太确定如何将该行的jobdetails中存储的日期与回显的日期进行比较?。

编辑&gt;&GT;&GT;&GT;&GT;

感谢下面的答案,iv设法提出以下内容,它回复了日期框,但没有引入任何数据,所以我不确定我的sql部分是否正确?。

            echo '<div class="dateboxcontainer">';
     $eventdata = <<<SQL
        SELECT *
        FROM `jobdetails`
    SQL;
    if(!$events = $db->query($eventdata)){
        die('There was an error running the query [' . $db->error . ']');
    }
    // read first event
    if ($nextEvent = mysql_fetch_assoc($events)) { // here is the first one
        extract($nextEvent); // prepare its variables
        // use the event date it to control the inner loop
        $nextDate = $datedroppingoff;
    } else // no events?
        $nextDate = 0; // prepare a fake date value

    // calculate today date
    $currentDate = mktime();

    // loop on the dates for the next 7 days
    for ($i = 0 ; $i < 7; $i++) {

        $currentEvents = "";

        // loop to print every event for current day (first one already extracted)
        while ($nextDate == date("Y-m-d", $currentDate)) { // next event occurs today

            // here prepare the var containing the event description
            // BTW, I'd use a list for the events
            $currentEvents .= "· $name<br>"; // use every field you need from current DB row

            // read next event
            if ($nextEvent = mysql_fetch_assoc($events)) { // here is the next one
                extract($nextEvent); // prepare its variables
                // use the event date it to control the inner loop
                $nextDate = $datedroppingoff;
            } else // no more events?
                $nextDate = 0; // prepare a fake date value
        }

        echo "
            <div class='datebox'>
                <div class='topdate'>" . strtoupper(date("D d m Y", $currentDate)) . "</div>
                <div class='bottomdate'>$currentEvents</div>
            </div>";

        $currentDate = strtotime("+1 day", $currentDate);
    } 
    echo '</div>';

4 个答案:

答案 0 :(得分:1)

你能用这样的东西:

SELECT * FROM `jobdetails` WHERE (`datedroppingoff ` > '2013-07-01 00:00:00' AND `datedroppingoff `  '2013-07-02 00:00:00') OR  (`datepickingup ` > '2013-07-01 00:00:00' AND `datepickingup `  '2013-07-02 00:00:00');

这必须每天重复(即5天中的每一天)

要做一个很好的循环,请使用填充了接下来的5个日期的数组作为字符串。然后在日期上做一个foreach并运行此查询。

答案 1 :(得分:1)

如果您想要从现在起和接下来的5天内的所有日期,您可以使用:

WHERE
    `datedroppingoff` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY) OR
    `datepickingup ` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY)

答案 2 :(得分:1)

让我们看看我是如何进行的(顺便说一句,这是我的第一个答案,所以我有点兴奋......)

首先,对这个超快脚本的一些基本假设(对不起,现在没有那么多时间来彻底测试它)。

  • 您的约会表中有一个包含约会日期的字段
  • 您的结果资源($ events)仅包含当前周的行
  • 生成的行按日期字段排序,升序(从最旧到最新)

试试这个(我原来的代码改了一下,对不起)

// read first event
if ($nextEvent = mysql_fetch_assoc($events)) { // here is the first one
    extract($nextEvent); // prepare its variables
    // use the event date it to control the inner loop
    $nextDate = $DB_field_with_event_date;
} else // no events?
    $nextDate = 0; // prepare a fake date value

// calculate today date
$currentDate = mktime();

// loop on the dates for the next 7 days
for ($i = 0 ; $i < 7; $i++) {

    $currentEvents = "";

    // loop to print every event for current day (first one already extracted)
    while ($nextDate == date("Y-m-d", $currentDate)) { // next event occurs today

        // here prepare the var containing the event description
        // BTW, I'd use a list for the events
        $currentEvents .= "· $your_desc_field<br>"; // use every field you need from current DB row

        // read next event
        if ($nextEvent = mysql_fetch_assoc($events)) { // here is the next one
            extract($nextEvent); // prepare its variables
            // use the event date it to control the inner loop
            $nextDate = $DB_field_with_event_date;
        } else // no more events?
            $nextDate = 0; // prepare a fake date value
    }

    echo "
        <div class='datebox'>
            <div class='topdate'>" . strtoupper(date("D d m Y", $currentDate)) . "</div>
            <div class='bottomdate'>$currentEvents</div>
        </div>";

    $currentDate = strtotime("+1 day", $currentDate);
} 

在假数据上尝试了几次,它应该可行。恕我直接更好地直接别名日期字段来直接从DB获取nextDate var,所以避免行,

            $nextDate = $DB_field_with_event_date;

答案 3 :(得分:0)

我最终用这个似乎做了这个工作! :)

    // Date box container
    echo '<div class="dateboxcontainer">';
    // Loop through and create a date for the next 7 days
    $days = new DatePeriod(new DateTime, new DateInterval('P1D'), 7); 
    foreach ($days as $day) {
    echo '<div class="datebox">';
    echo '<div class="topdate">';

          echo strtoupper($day->format('D d')) . PHP_EOL;
    echo '</div>';


              // Get the names for each day
          $theday = strtoupper($day->format('Y-m-d'));    
          $sqldate = <<<SQL
        SELECT *
        FROM `jobdetails`
        WHERE datedroppingoff = '$theday' OR datepickingup = '$theday'
    SQL;
    if(!$resultdate = $db->query($sqldate)){
        die('There was an error running the query [' . $db->error . ']');
    }
    while($rowdate = $resultdate->fetch_assoc()){
        echo $rowdate['name'];
            }
         //

    echo '</div>';
    }  
    echo '</div>';
    //