0

I want to hide the time frame (00:00:00) which is appearing in the result of the query below in the columns "Start Date" and "End Date".

Result of the query is displaying this way:

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

The code looks like this:

$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
  • 1
    Just an observation, but you don't query for City/University. Probably just a typo here, but wanted to make sure. It looks as though you are using mysql DATETIME or TIMESTAMP datatype. I personally prefer to store my times in php's native seconds since epoch, in an int datatype column. This facilitates easier manipulation with php's built in date function. Commented Feb 23, 2012 at 23:48
  • I agree unixtime is much easier to work with! Commented Feb 23, 2012 at 23:49
  • You could do with doing some mysql_real_escape_string on your concatenated variables too! Commented Feb 23, 2012 at 23:54

3 Answers 3

2

Try this:

$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";
Sign up to request clarification or add additional context in comments.

Comments

2

You need to format the date they are two methods

Using SQL

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

Or

Using PHP

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

You can include that in a PHP function

Comments

0

You could use explode() as you might want to use the time component one day! e.g:

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

Then access these parts as:

$row['Start Date'][0]

Will be: 2011-12-28

$row['Start Date'][1]

Will be: 00:00:00

$row['End Date'][0]

Will be: 2012-02-03

$row['End Date'][1]

Will be: 00:00:00

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.