I have a date as follows.
$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 into MySql? I'm from Java background and very new to PHP.
The parse() method takes a date string (such as "2011-10-10T14:48:00" ) and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This function is useful for setting date values based on string values, for example in conjunction with the setTime() method and the Date object.
PHP date() Function The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. record the last updated a data in a database.
Don't use DateTime
. The normal php way of doing this sort of thing is to use date()
and strtotime()
;
$discount_start_date = '03/27/2012 18:47';
$start_date = date('Y-m-d H:i:s', strtotime($discount_start_date));
You don't actually need to convert it into a string. MySQL has a date, time, datetime, as well as timestamp native data types. You should be able to simply insert the date immediately without casting it to a string so long as you insert it into one of these fields and have it formatted properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With