简体   繁体   English

检查日期是否在日期范围内

[英]check if date is within a date range

I have a database filled with a bunch of jobs, where I have startDate and endDate. 我有一个充满一堆工作的数据库,其中有startDate和endDate。 What Im trying to do is to list current week and 20 weeks ahead, and see if those weeks are within the database. 我想做的是列出当前周和未来20周,并查看这些周是否在数据库中。 And when I echo it out I want the date to be green if the date exists and black if not. 当我回显它时,如果日期存在,我希望日期为绿色,否则为黑色。

It works, but the problem is that it skips the first week. 可以,但是问题是它跳过了第一周。 So if I have 2014-07-01 as startDate and 2014-07-14 as endDate, then the second week in that example will get green, not the first one. 因此,如果我将2014-07-01作为startDate并将2014-07-14作为endDate,则该示例中的第二周将变为绿色,而不是第一周。

This is what I'v done: 这是我所做的:

UPDATED 更新

$last = 19;
$today = new \DateTime();
$today->modify('Monday this week');

for ($i=0; $i<$last; $i++) {
  $the_week = $today->format('o-m-d');

  $sql = mysql_query("SELECT startDate,endDate FROM work WHERE '$the_week' BETWEEN startDate AND endDate'");
  $row = mysql_fetch_array($sql);

  $startdate = $row['startDate'];
  $enddate = $row['endDate'];

if(($startdate > $the_week) AND ($enddate < $the_week)) { 
 $color = "#69dd54;"; } else { $color = "#ffffff;"; }

    echo "<span style='color: ". $color ."'>". $the_week ."</span>";
    $new_today->modify('next Monday');
}

this is the output of your foreach loop for $the_week 这是$ the_week foreach循环的输出

2014-07-09
2014-07-16
2014-07-23
2014-07-30
2014-08-06
2014-08-13
2014-08-20
2014-08-27
2014-09-03
2014-09-10
2014-09-17
2014-09-24
2014-10-01
2014-10-08
2014-10-15
2014-10-22
2014-10-29
2014-11-05
2014-11-12
2014-11-19

as you can see the second one is outside of your date range 如您所见,第二个不在您的日期范围内

Try this as your loop 试试这个作为你的循环

    //$first = 0;
    $last = 19;
    //$weeks = range($first, $last);
    $today = new \DateTime();
    $today->modify('Monday this week');

    for ($i=0; $i<$last; $i++) {        
        $the_week = $today->format('o-m-d');
        $sql = mysql_query("SELECT startDate,endDate FROM work WHERE $the_week BETWEEN startData AND endDate"); //what happend to your original query.
        $r = mysql_num_rows($sql);
        $row = mysql_fetch_array($sql);

        $startdate = $row['startDate'];
        $enddate = $row['endDate'];

       if(($startdate > $the_week) AND ($enddate < $the_week)) { 
           $color = "#69dd54;"; } else { $color = "#ffffff;";
       }

    echo "<span style='color: ". $color ."'>". $the_week ."</span>";
        $today->modify('next Monday');
    }

outputs 输出

2014-07-07
2014-07-14
2014-07-21
2014-07-28
2014-08-04
2014-08-11
2014-08-18
2014-08-25
2014-09-01
2014-09-08
2014-09-15
2014-09-22
2014-09-29
2014-10-06
2014-10-13
2014-10-20
2014-10-27
2014-11-03
2014-11-10

Using DateTime you can increment by the 'next Monday' or any day for that matter, with a simple for loop. 使用DateTime,您可以通过一个简单的for循环在下一个星期一或任何一天进行递增。

also make sure to change 也要确保改变

$startdate = $ro['startDate'];
  $enddate = $ro['endDate'];

to

$startdate = $row['startDate'];
  $enddate = $row['endDate'];

What is $the_week ? 什么是$the_week It appears to be a date, but there are many dates in a week. 它似乎是一个日期,但是一周中有很多日期。 For example there is the first date of a week and the last date of the week. 例如,有一周的第一个日期和一周的最后一个日期。 It could be that you have a problematic boundary condition here. 可能是您在这里的边界条件有问题。

You want the start date to be before the last day of a week, and the end date to be after the first day of a week, I am assuming. 我想,您希望开始日期 一周的最后一天 之前 ,而结束日期 一周的第一天 之后 Therefore the first week gets excluded because its first day is before the start date, and yet its last day is after the start date. 因此,第一周被排除在外,因为它的第一天在开始日期之前,而最后一天在开始日期之后。

UPDATE: 更新:

Consider having: 考虑具有:

$the_week = date('o-m-d', strtotime($today. ' + '. $week .' week'));
$the_week_last = date('o-m-d', strtotime($today. ($week +1) .' week'));

By the way, I would recommend you do a single SQL query for this, rather than one for every week. 顺便说一句,我建议您对此执行一次SQL查询,而不是每周执行一次。 There is an example of how to do that here . 还有就是如何做到这一点的例子在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM