简体   繁体   中英

How can i select the time datetime in zf2 db sql

I am trying to get the time from datetime format, how can I achieve this.

I know that to concat the string we can sql/expression to achieve the concatenation but how can I get the time.

"full_name" => new \Zend\Db\Sql\Expression(
    "CONCAT(?, ' ', ?)", array(
        'first_name' => 'users.first_name', 
        'last_name' => 'users.last_name'
    )
)

You can simply format your time afterward from the row in the result set like this:

With php DateTime class:

$date = $row['date'];
$dateTime = new DateTime($date);
$formatted = $dateTime->format("d-m-Y H:i:s");
echo($formatted); //output format: 01-01-2015 01:30:00

For time only

$time = $dateTime->format("H:i:s");
echo($time); //output format: 01:30:00

With strtotime function:

$date = $row['date'];
$formatted = date("d-m-Y H:i:s", strtotime($date));
echo($formatted); //output format: 01-01-2015 01:30:00

For time only

$time = date("H:i:s", strtotime($date));
echo($time); //output format: 01:30:00

Check this php reference page for all formatting options.

You can use the TIME function in Mysql

"time_col" => new \Zend\Db\Sql\Expression(
    "TIME(?)", array(
        'time_col' => 'table.time_col',
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM