【问题标题】:Issue in get the start and end dates of all weeks between two dates in Mysql获取Mysql中两个日期之间所有周的开始和结束日期的问题
【发布时间】:2015-08-03 22:57:37
【问题描述】:

我想显示选择开始日期到结束日期之间的所有星期日期,所以我尝试以下代码

 <? 

 $signupweek='2015-05-21';
 /*start day*/
 for($i = 0; $i <7 ; $i++)
{
 $date = date('Y-m-d', strtotime("-".$i."days", strtotime($signupweek)));
 $dayName = date('D', strtotime($date));
 if($dayName == "Sun")
 {
   echo "start day is ". $date."<br>";
 }
}
?>

当我在上面运行代码时,它只给出开始日期前 7 天的日期,但我希望得到如下结果

sDate = '2013-02-25',
eDate = '2013-03-25';

输出

2013-02-25                
2013-03-04                 
2013-03-11                

知道我怎样才能使它成为可能吗?你的建议是可观的。

编辑

根据 someOne 的回答,我得到了我想要的结果,但现在我对此进行了一些更改,现在如果我输入 start_date =2015-04-01 和 end_date=2015 -05-01 所以我需要像下面这样的输出

2015-04-08
2015-04-15
2015-04-22
2015-04-29

我的输出列表中不需要 start_date=2015-04-01 。是否可以在以下函数中使用?

【问题讨论】:

  • 这对您有帮助吗? stackoverflow.com/questions/712761/…
  • Anthony Lopez:我有 start_dateend_date ,所以我需要 这两个日期之间的所有星期日期,但在您的参考链接中,根据我的要求,没有任何合适的解决方案。
  • WHERE (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55') 因为您标记为 mysql,这只是一个示例。谷歌“mysql 之间的日期”,你会发现一大堆结果。然后是mysqltutorial.org/mysql-between,还有更多示例。
  • Fred -ii:我知道我可以在两个日期之间获取数据,但我的要求有所不同我只需要所有星期日期值,而不是我的 start_date 和 end_date 中的所有日期值
  • 您不认为您在所需的输出中错过了'2013-03-18' 甚至可能是'2013-03-25'

标签: php mysql date


【解决方案1】:

根据OP的编辑和请求,提供了以下新功能,使用户可以使用$includeFrom的可选参数指定是否在最终结果中包含开始或结束日期和$includeTo分别:

function getWeeklyDates2($from, $to, $includeFrom = false, $includeTo = true) {
    $from = strtotime($from);
    $to = strtotime($to);

    if($includeFrom===false)
        $from = strtotime("+7 day", $from);

    $dates = array();

    $current = $from;
    while($includeTo ? $current <= $to : $current < $to) {
        $dates[] = $current;
        $current = strtotime("+7 day", $current);
    }
    return $dates;
}

$dates2 = getWeeklyDates2('2015-04-01', '2015-04-29', false, true);
foreach($dates2 as $date)
    echo date("Y-m-d", $date) ."<br />";

【讨论】:

    【解决方案2】:

    所以你想得到下周的日期:

    <?php
    function getWeeklyDates($from, $to) {
        $from = strtotime($from);
        $to = strtotime($to);
    
        $dates = array();
        $current = $from;
        while($current <= $to) {
            $dates[] = $current;
            $current = strtotime("+7 day", $current);
        }
        return $dates;
    }
    
    $dates = getWeeklyDates('2013-02-25', '2013-03-25');
    foreach($dates as $date)
        echo date("Y-m-d", $date) ."<br />";
    ?>
    

    【讨论】:

    • @HarshalKalavadiya 关于对 StackOverflow 的评论,您可能会发现 thisthis 很有用,欢迎您:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2019-04-23
    • 1970-01-01
    相关资源
    最近更新 更多