如何打印2个日期之间的所有日期

时间:2019-01-22 08:55:07

标签: php

我确定是否有可能,所以我会与您协商。

我有2个包含日期的字符串:d / m / Y

立即:

$from_date= "17/10/2018";

$till_date = "20/10/2018";

是否可以打印这些日期之间的总天数并打印日期?

瞬时:

4天

17/10/2018

18/10/2018

19/10/2018

20/10/2018

4 个答案:

答案 0 :(得分:4)

这应该有效:

$start = new DateTime('2018-10-17');
$end = new DateTime('2018-10-20');

while ($start <= $end) {
    echo $start->format('Y-m-d') . '<br>';
    $start->modify('+1 day');
}

答案 1 :(得分:1)

使用DateTime类及其关联的方法createFromFormat将提供的日期转换为可行的格式,并添加DateInterval可以使此工作变得很顺畅

$int    = new DateInterval('P1D');
$start  = DateTime::createFromFormat('d/m/Y','17/10/2018')->sub( $int );
$end    = DateTime::createFromFormat('d/m/Y','20/10/2018');


while( $start < $end )echo $start->add( $int )->format('Y-m-d') . '<br />';

输出:

2018-10-17
2018-10-18
2018-10-19
2018-10-20

要获取指定的输出格式,请更改格式方法中提供的format字符串-即:->format('d-m-Y')

答案 2 :(得分:0)

  

仅用于帮助实施。不完整描述

您可以使用此代码

get_max.cpp: In function 'int getMax(int*)':
get_max.cpp:6:17: error: 'begin' was not declared in this scope
     for (int x: arr){
                 ^
get_max.cpp:6:17: note: suggested alternative:
In file included from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/string:51:0,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/locale_classes.h:40,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/ios_base.h:41,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ios:42,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ostream:38,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/iostream:39,
                 from get_max.cpp:1:
C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/range_access.h:87:5: note:   'std::begin'
     begin(_Tp (&__arr)[_Nm])
     ^
get_max.cpp:6:17: error: 'end' was not declared in this scope
     for (int x: arr){
                 ^
get_max.cpp:6:17: note: suggested alternative:
In file included from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/string:51:0,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/locale_classes.h:40,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/ios_base.h:41,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ios:42,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ostream:38,
                 from C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/iostream:39,
                 from get_max.cpp:1:
C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/range_access.h:97:5: note:   'std::end'
     end(_Tp (&__arr)[_Nm])

第二天直到到达echo date('Y-m-d', strtotime($from_date. ' + 1 days'));

答案 3 :(得分:-1)

是的,您可以尝试这样的事情:

$arr = array("17-10-2018", "18-10-2018", "19-10-2018", "20-10-2018");
$countDateBetweenDates = 0;

foreach ($arr as $value) {

    if (strtotime($value) > strtotime("17-10-2018") && strtotime($value) < strtotime("20-10-2018")) {

        $countDateBetweenDates = $countDateBetweenDates + 1;
    }       

}

echo $countDateBetweenDates;