Formatting an SQL timestamp with PHP

后端 未结 6 550
無奈伤痛
無奈伤痛 2020-12-01 16:15

I have a mySQL database with a timestamp field. It currently only has one entry while I\'m testing, it is

2010-02-20 13:14:09

I am pulling

6条回答
  •  囚心锁ツ
    2020-12-01 16:46

    The date function expects an UNIX timestamp as its second parameter -- which means you have to convert the date you get from the DB to an UNIX timestamp, which can be done using strtotime :

    $db = '2010-02-20 13:14:09';
    $timestamp = strtotime($db);
    echo date("m-d-Y", $timestamp);
    

    And you'll get :

    02-20-2010
    


    You were passing the '2010-02-20 13:14:09' string to the date function ; that string is not a valid UNIX Timestamp.

    '12-31-69' is probably 1970-01-01, in your locale ; and 1970-01-01 is the Epoch -- the date that corresponds to the 0 UNIX Timestamp.

提交回复
热议问题