How to convert date-time to string in PHP?

I have the following date.

$discount_start_date='03/27/2012 18:47'; $start_date=new DateTime($discount_start_date); $start_date->format('Y/m/d H:i:s'); 

How can I convert it to a string in PHP so that it can be stored in MySql? I am from Java background and very new to PHP.

+4
source share
2 answers

Do not use DateTime . The usual php way of doing such things is to use date() and strtotime() ;

 $discount_start_date = '03/27/2012 18:47'; $start_date = date('Ymd H:i:s', strtotime($discount_start_date)); 
+4
source

In fact, you do not need to convert it to a string. MySQL has a date, time, date and time, as well as its own timestamp data types. You should just insert the date right away without dropping it on the string while you paste it into one of these fields and format it correctly.

+1
source

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


All Articles