PHP Day(l)在While循环中没有显示正确的一天

时间:2015-08-19 11:09:23

标签: php

我从数据库中获取数据并在表格中显示这些数据。 获取正常工作的所选日期范围之间的日期。

while ($row = mysql_fetch_array($query)) {

 $date = $row['tt_date']; //get dates from table column

echo $date; // showing proper values

}

当我添加

echo date('l', strtotime( $date)); 

 echo date('l',$date);

对于从表中检索的所有数据,它一直显示星期四;

它假设显示检索日期的正确日期。

1 个答案:

答案 0 :(得分:1)

根据OP评论,似乎$date变量包含多个日期,因此OP需要展开该字符串,并将.字符更改为-循环:

    $date = '2015.08.31 | 2015.08.31 | 2015.09.02';
    echo $date;
    //Will output 2015.08.31 | 2015.08.31 | 2015.09.02
    //So now here, you need to explode this string with " | "
    $datesArray = explode(" | ", $date);
    var_dump($datesArray);
    //The output is:
    //array (size=3)
    //  0 => string '2015.08.31' (length=10)
    //  1 => string '2015.08.31' (length=10)
    //  2 => string '2015.09.02' (length=10)
    //Now you need to iterate through on this array
    foreach ($datesArray as $dateString) {
        //You need to replace the "." character to "-" for the strtotime function
        echo date('l', strtotime( str_replace('.','-',$dateString))) ."<br>\n";
    }
    //Output is:
    //Monday
    //Monday
    //Wednesday
    //I've checked the calendar, and it's correct