dongqiangse6623 2018-05-01 17:48
浏览 33

日期在PHP中的2个日期没有正确显示

Below code doesn't show the output of the month of 05 as input but if the date range is big then it shows the correct info.

//Code for finding All the Dates between TWO Date
//Code for finding month between two Date
$startDate = new DateTime("2018-04-26");
$inputEndDate = new DateTime("2018-05-03");
$inputEndDate->modify('+1day');//for increasing the day by 1

//for counting the month
$monthInterval = new DateInterval('P1M');
$monthPeriod   = new DatePeriod($startDate, $monthInterval, $inputEndDate);
$monthCount = 0;

foreach ($monthPeriod as $date) {
$endDate = new DateTime(date("Y-m-t", strtotime($date->format('Y-m-d')))); 

//last day of the month

$endDate->modify('+1 day');//for increasing the day by 1
if ($inputEndDate < $endDate) {
    $endDate = $inputEndDate;
}


//var_dump($endDate);

$dateRange = new DatePeriod($startDate, new DateInterval('P1D'), $endDate);
$startDate = $endDate->add(new DateInterval('P1D'))->modify('-1 day');


echo "{$date->format('F')}-{$date->format('Y')}";
foreach($dateRange as $dateHeader){
    echo $dateHeader->format("Y-m-d");
}
$monthCount++;}

Output:

April-2018
2018-04-26
2018-04-27
2018-04-28
2018-04-29
2018-04-30

The output does not show the result of another month as in input.

  • 写回答

1条回答 默认 最新

  • douhaoqiao9304 2018-05-01 18:21
    关注
    function get_dates_through_range($start, $end, $format = 'Y-m-d')
    {
        $range = array();
        $interval = new DateInterval('P1D');
    
        $range_end = new DateTime($end);
        $range_end->add($interval);
    
        $period = new DatePeriod(new DateTime($start), $interval, $range_end);
    
        foreach($period as $date) {
            $range[] = $date->format($format);
        }
    
        return $range;
     }
    

    get_dates_through_range('2018-04-30','2018-05-03')

    Outputs

    array(4) {
        [0]=>
        string(10) "2018-04-30"
        [1]=>
        string(10) "2018-05-01"
        [2]=>
        string(10) "2018-05-02"
        [3]=>
        string(10) "2018-05-03"
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度