How to format date from timestamp in PHP?

I have a database row whose type is "timestamp". It is automatically filled every time a new record is added with the time of making this record. Right now, for one of the entries, it contains the value:

2010-06-19 18:40:51

I tried to include this in a more convenient format:

$timeStamp = $row['date'];
$timeStamp = date( "m/d/Y", $timeStamp);

But he returns this:

12/31/1969

I read somewhere that the date function can only be used on unix timestamps, maybe what I have in my database is not a suitable timestamp, and I need to convert it to the first one before using the date function on it (?), what is the solution?

+4
source share
4 answers

strtotime:

$timeStamp = $row['date'];
$timeStamp = date( "m/d/Y", strtotime($timeStamp));

2010-06-19 18:40:51 :

06/19/2010


timestamp , , DEFAULT CURRENT_TIMESTAMP . :

CREATE TABLE TIMESTAMP :

DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP .

DEFAULT ON UPDATE , DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.

DEFAULT CURRENT_TIMESTAMP ON UPDATE , .

+5

strtotime($timeStamp) (-) - , unix.

, - date( "m/d/Y", strtotime($timeStamp) )

http://php.net/manual/en/function.strtotime.php

+2

SELECT UNIX_TIMESTAMP(sql_row_with_timestamp) AS some_much_more_readable_name FROM your_table .

0

Source: https://habr.com/ru/post/1750930/


All Articles