简体   繁体   中英

php converting string into datetime format

I am having problems converting a string into datetime format in order to store variable into a datetime-type field of a sql table

value of $timestamp: 2019-02-23T08:30:03.77

$datum2 = substr($timestamp,0,19);
$datum2 = str_replace('T',' ', $datum2);
echo $datum2 ."<br>";

--> 2019-02-23 08:30:03  .... echo output looks ok to me

$datum2 = date_format($datum2,'Y-m-d H:i:s');

--> error message

Warning: date_format() expects parameter 1 to be DateTimeInterface

Thank you for any hints Stefan

Check below code:

$date = '2019-02-23 08:30:03'; 
$datum2 = date('Y-m-d H:i:s', strtotime($date));

You can convert string date to timestamp and later can change to date format. Hope it helps you.

您必须在$timestamp上使用date_create函数:

$datum2 = date_format(date_create($timestamp),'Y-m-d H:i:s');

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