Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datepicker time to mysql datetime format in PHP

Tags:

php

mysql

Is there a way I can convert a date in this format: 08/19/2014 1:45 pm

into MySQL datetime format like 2014-08-19 13:45:00.

I tried using something like

date("Y-m-d H:i:s", $myTime);

but I don't think it likes the 'pm' and gives back 1969-12-31, giving error:

"A non well formed numeric value encountered"

like image 286
user2994560 Avatar asked Dec 04 '25 19:12

user2994560


2 Answers

Have you tried using strtotime()?

$myTime = strtotime("08/19/2014 1:45 pm"); 
echo date("Y-m-d H:i:s", $myTime);

Output:

2014-08-19 13:45:00
like image 88
Erlesand Avatar answered Dec 07 '25 07:12

Erlesand


This works on dd/mm/yyyy

$date = '18/03/2016 16:25';
echo date("Y-m-d H:i:s",strtotime(str_replace('/','-',$date)));

=> 2016-03-18 16:25:00
like image 42
Fury Avatar answered Dec 07 '25 09:12

Fury



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!