PHP Convert Date and Time Strings to MySQL DateTime

I have two text fields: one for a date in the format 01/08/2012 and a second field containing the time. Currently, I have a time field in the format 09:41, but I have some flexibility with its format (if, for example, 24 hours is easier).

I planned to only concatenate strings and then convert. Should I first convert the date to 2012-08-01?

How can I finish the conversion to datetime (2012-08-01 09:41:00)? How to convert back from it to the format 01/08/2012 and 09:41 AM?

+5
source share
2 answers
SELECT STR_TO_DATE(concat('08/01/2012', '09:41am'),'%d/%m/%Y%h:%i');
+6
source

On the database side you can use:

STR_TO_DATE(), , .

DATE_FORMAT() , .

PHP :

  • strtotime() - Unix
  • date - /

strotime Unix, /, . (08/01/2012 09:41), . , date("m/d/Y H:ia").

$field1 = '08/01/2012';
$field2 = '09:41am';

$stamp = strtotime($field1 . ' ' . $field2);

echo date("m/d/Y H:ia", $stamp);

, .

+2

All Articles