I'm trying to convert a variable named $releasedate
which contains the string 'May 31, 2011'
to the string '2011-05-31 00:00:00'
to insert into the database.
I have tried...
$pubdate = date('Y-m-d H:i:s',$releasedate);
...but it didn't work.
How do I do this?
You can use strtotime()
.
$pubdate = date('Y-m-d H:i:s', strtotime($releasedate)); // 2011-05-31 00:00:00
CodePad .
strtotime is the quick/easy version, and for a proper "english" date like this, it'd work fine. But it can quite easily choke on something like "01/02/03" (March 2, 2001? Feb 1st, 2003?). As well, there is a fair amount of parseing overhead to try and figure out what the string really is. If you know in advance what the date's format is, you'd be better off using [date_create_from_format()][1]
:
$time = date_create_from_format('M j, Y', $release_date);
$formatted = $time->format('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.