-2

If i have a string with format "MM-YYYY" like 04-2016, how to get them into datetime format mysql which is "YYYY-MM-dd hh:mm:ss".

I wonder like this : 2016-04-01 00:00:00, I am using php with mysql.

Fadly Dzil
  • 2,154
  • 3
  • 34
  • 85
  • post your code in question – Chetan Ameta Apr 01 '16 at 10:23
  • 2
    Possible duplicate of [Storing datetime as UTC in PHP/MySQL](http://stackoverflow.com/questions/1349280/storing-datetime-as-utc-in-php-mysql) – alexander.polomodov Apr 01 '16 at 10:27
  • 1
    `$string = '04-2016'; $dto = DateTime::createFromFormat('d-m-Y H:i:s', '01-' . $string . ' 00:00:00'); echo $dto->format('Y-m-d H:i:s');` – Mark Baker Apr 01 '16 at 10:27
  • Possible duplicate of [Managing date formats differences between PHP and MySQL](http://stackoverflow.com/questions/415092/managing-date-formats-differences-between-php-and-mysql) – dubes Apr 01 '16 at 11:59

2 Answers2

1
$d = '04-2016';

$date = DateTime::createFromFormat('d-m-Y', ('01-'.$d));
$date->setTime(0, 0, 0);

echo $date->format('Y-m-d H:i:s'); // Outputs 2016-04-01 00:00:00

Something like the above will allow you to get the format you want in PHP, then you just have to insert it into your database as you currently are.

Mikey
  • 2,606
  • 1
  • 12
  • 20
0

you have date time formate with Am and pm then use below code

 $d='03-03-2016 03:07:22';

echo date('Y-m-d h:i:s a',strtotime($d));
Digpal Singh
  • 166
  • 1
  • 5