0

I am trying to write a script to pull dates for the next 7 days and put them into a div for each date :

    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

Im now trying to pull data from my database from two fields 'datedroppingoff' and 'datepickingup', which are formatted like this '2013-07-10 14:29:28'.

Im kind of stuck though as im not sure what query to write to put the appointments for each day into each day div where 'some info' currently sits.

Im guessing it would be something like

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

but im not quite sure how I can compare the date stored in jobdetails for that row to the date being echo'd out ?.

Edit>>>>>

Thanks for the answers below, iv managed to come up with the following, it echos out the date boxes ok, but doesnt bring in any data, so im not sure if I have the sql part correct ?.

            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

4 回答 4

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 并运行此查询。

于 2013-07-22T14:53:33.943 回答
1

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

WHERE
    `datedroppingoff` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY) OR
    `datepickingup ` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY)
于 2013-07-22T14:54:08.773 回答
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);
} 

在假数据上尝试了几次,它应该可以工作。恕我直言,最好直接别名日期字段以直接从数据库中获取 nextDate var,从而避免行,

            $nextDate = $DB_field_with_event_date;
于 2013-07-22T18:21:58.763 回答
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>';
    //
于 2013-07-23T09:14:53.083 回答