隐藏PHP MySQL查询结果中的字符

时间:2012-02-23 23:36:14

标签: php mysql

我想隐藏出现在查询结果中的时间范围(00:00:00) 在下面的“开始日期”和“结束日期”列中。

查询结果以这种方式显示:

Department            City/University   Start Date                      End Date
1. Building Science   Miami          2011-12-28 00:00:00            2012-02-03 00:00:00

代码如下所示:

$date_start = firstOfMonth();
$date_end  = lastOfMonth();

$query = " SELECT Department, `Start Date`, `End Date` FROM facultytravel WHERE Country='".$country."' AND `Start Date`<='".$date_end."' AND `End Date`>='".$date_start."' ORDER BY `Start Date` ASC";
            $result = mysql_query($query);

            $num = 1;
            while($row = mysql_fetch_array($result)){                   
                print "<tr>";
                print "<td>".$num.". ".$row['Department']."</td>";
                print "<td>".$row['City/University']."</td>";
                print "<td>".$row['Start Date']."</td>";
                print "<td>".$row['End Date']."</td>";
                print "</tr>";
                $num++;
            }

3 个答案:

答案 0 :(得分:2)

您需要格式化两种方法的日期

使用SQL

SELECT DATE_FORMAT(`End Date`, '%Y-%M-%D');

使用PHP

$time = strtotime($your_query['End Date']);
$date = date("Y-m-d",$time ); 

您可以将其包含在PHP函数中

答案 1 :(得分:2)

试试这个:

$query = " SELECT Department, CAST(`Start Date` AS DATE) as `Start Date`, CAST(`End Date` AS DATE) AS `End Date` FROM facultytravel WHERE Country='".$country."' AND `Start Date`<='".$date_end."' AND `End Date`>='".$date_start."' ORDER BY `Start Date` ASC";

答案 2 :(得分:0)

您可以使用explode(),因为您可能希望有一天使用时间组件! e.g:

$start_datetime = explode(' ', $row['Start Date']);
$end_datetime = explode(' ', $row['End Date']);

然后访问以下部分:

$row['Start Date'][0]

将是:2011-12-28

$row['Start Date'][1]

将是:00:00:00

$row['End Date'][0]

将是:2012-02-03

$row['End Date'][1]

将是:00:00:00