无法在php中打印给定日期范围内的日期

时间:2017-02-01 06:36:59

标签: php datetime

我正在尝试打印我从数据库获取的2个日期值之间的日期范围。但是当我打开页面时,我收到错误Catchable fatal error: Object of class DateTime could not be converted to string我该如何解决这个问题? 这是我到目前为止所做的事情。

<?php       
        $getstartdate = getProjectstartdate($projectId);
        $getenddate = getProjectenddate($projectId);
        $begin = new DateTime($getstartdate);
        $end = new DateTime($getenddate);
        $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
        foreach($daterange as $date){
            echo $end;
            echo $begin;
            $dte = $date->format("Y-m-d");
            $day = date('D', strtotime($dte));
            echo '<a href="create-session-for.php?date='.$dte.'"><div class="'.$day.' date-box"><b>'.$day.'</b><br>'.$dte.'</div></a>';
        }       
    ?>

的functions.php

function getProjectstartdate($project_id){
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT project_start_date FROM projects WHERE id='$project_id'";
    $result = $conn->query($sql);      
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {              
            $starts_by = $row["project_start_date"];
            return $starts_by;
        }
    }   
    $conn->close();   
}

function getProjectenddate($project_id){
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT project_end_date FROM projects WHERE id='$project_id'";
    $result = $conn->query($sql);      
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {              
            $ends_by = $row["project_end_date"];
            return $ends_by;
        }
    }   
    $conn->close();
}

1 个答案:

答案 0 :(得分:-1)

使用投射操作符(string)$yourObject;