Extracting only date from datetime field (mysql) and assigning it to php variable

前端 未结 3 1320
礼貌的吻别
礼貌的吻别 2021-02-08 19:31

I am unable to assign only date to a php variable from datetime field of a MySQL table. I am using PHP 4, I know how to do it in PHP 5.

Can

相关标签:
3条回答
  • 2021-02-08 19:57

    If you just want to display the date portion, you could use the DateTime class:

    echo DateTime::createFromFormat("Y-m-d H:i:s", "2012-12-24 12:13:14")->format("d/m/Y");
    // 24/12/2012
    

    //Edited for double echo typo.

    0 讨论(0)
  • 2021-02-08 20:04

    There is a link I find extremely useful when it comes to date manipulations: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

    one can easily find the solution there:

    SELECT *, DATE(Invdate) as Inv_date ...
    
    0 讨论(0)
  • 2021-02-08 20:12

    Do it directly in MySQL:

    select DATE_FORMAT('2012-12-24 12:13:14', '%Y/%m/%d')
    

    So your query will look like this:

    SELECT DATE_FORMAT(Invdate, '%Y/%m/%d') FROM INVHDR WHERE Invdate BETWEEN '$startdat' AND '$enddat'
    
    0 讨论(0)
提交回复
热议问题