PHP Zend date format

后端 未结 6 1617
粉色の甜心
粉色の甜心 2021-02-19 08:12

I want to input a timestamp in below format to the database.

yyyy-mm-dd hh:mm:ss

How can I get in above format?

When I

6条回答
  •  灰色年华
    2021-02-19 08:42

    Technically, @stefgosselin gave the correct answer for Zend_Date, but Zend_Date is completely overkill for just getting the current time in a common format. Zend_Date is incredibly slow and cumbersome to use compared to PHP's native date related extensions. If you don't need translation or localisation in your Zend_Date output (and you apparently dont), stay away from it.

    Use PHP's native date function for that, e.g.

    echo date('Y-m-d H:i:s');
    

    or DateTime procedural API

    echo date_format(date_create(), 'Y-m-d H:i:s');
    

    or DateTime Object API

    $dateTime = new DateTime;
    echo $dateTime->format('Y-m-d H:i:s');
    

    Don't do the common mistake of using each and every component Zend Frameworks offers just because it offers it. There is absolutely no need to do that and in fact, if you can use a native PHP extension to achieve the same result with less or comparable effort, you are better off with the native solution.

    Also, if you are going to save a date in your database, did you use any of the DateTime related columns in your database? Assuming you are using MySql, you could use a Timestamp column or an ISO8601 Date column.

提交回复
热议问题