Trying to convert a date & time value in the format of "07/19/2011 00:00" using the php function "strtotime()".
Doesn't seem to like the structure of the da开发者_StackOverflow中文版te time. Does anyone know how I can convert this to a timestamp for MySQL database?
$convertstart = strtotime($_POST['start']);
$start = mysql_real_escape_string($convertstart);
$convertend = strtotime($_POST['end']);
$end = mysql_real_escape_string($convertend);
Thanks!
EDIT also...
When I convert today's date( 07/31/2011 18:35 ) I get back 2013-12-15 17:00:00 in the MySQL
Why not let MySQL take care of it for you. Try out MySQL's STR_TO_DATE()
function. It uses the same format specifiers as the DATE_FORMAT()
function.
Try this: SELECT STR_TO_DATE('07/31/2011 18:35', '%m/%d/%Y %H:%i');
It should give you a time-stamp compatible with MySQL DATETIME fields. For me it returns: 2011-07-31 18:35:00
That date format is too ambiguous to be parsed automatically. You'll need to parse that format explicitly using, for example, strptime
:
$p = strptime($date, '%m/%d/%Y %H:%i');
echo date('Y-m-d H:i:s', mktime($p['tm_hour'], $['tm_min'], 0, $p['tm_mon'], $p['tm_mday'], $p['tm_year']));
You could use PHP's date function combined with strotime() to create a valid MySQL-timestamp.
$sqlend = date('Y-m-d H:i:s', strtotime($end));
精彩评论