Sam Blake
Sam Blake

Reputation: 47

Issue converting DD/MM/YYYY hh:ii date to YYYY/MM/DD hh:ii MYSQL

Been searching for ages to try and find an answer for this but got no luck.

I have a column with a Datetime type. I have a field on a webpage which has an input control which puts the date in this format 20-12-2015 23:30 .When I try and store this I just get blank entries or 0000-00-00 00:00:000

The field likes the date in this format 2015-20-12 23:15:00

Can anybody help me please help me work out how to convert this correctly. I have been trying with several Date_Format methods but all give me an error

"1292 Incorrect datetime value: '10/12/2015 10:50'"

Upvotes: 0

Views: 288

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

If you have a fixed format date being returned from the page that is not a compatible date, and you cannot change the format used on the page, then before you attempt to store it on the database you need to convert it to a compatible format.

PHP has a DateTime object that does exactly this

<?php
$in = '20-12-2015 23:30';

$date = DateTime::createFromFormat('d-m-Y H:i', $in);
$to_db_date = $date->format('Y-m-d H:i:s');

echo $to_db_date;

This produces a MySQL compatible date string like

2015-12-20 23:30:00

Upvotes: 1

Related Questions