0

I have an variable which holds a date in UK format, ie 250711 which means the 25th July 2011

When I insert that into MYSQL it becomes 2025-07-11

How can I convert it before writing into MySQL such that is displayed correctly in MySQL as 2011-07-25?

Thanks alot,

Greg

3
  • 1
    I don't think "250711" is a date in any standard format ;) Commented Jul 28, 2011 at 11:42
  • but that's what I have to start with, it's come from the output of a solar panel invertor so I cannot change the source.... Commented Jul 28, 2011 at 11:43
  • 1
    @VolkerK got your solution, but If you need to process the date in PHP somewhere else in the code, you can use this strptime("250711", "%d%m%y") Commented Jul 28, 2011 at 11:48

2 Answers 2

6

If you want to use the string for an insert/update/select statement anyway you can let MySQL do the work

INSERT INTO ... STR_TO_DATE('250711','%d%m%y')

see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date

Sign up to request clarification or add additional context in comments.

Comments

0

Hey i hope it ain't toooo late to answer this; however it should help people who will view this question in the future

Just use this function:

function mysqlDate($input) {
  $output = false;
  $d = preg_split('#[-/:. ]#', $input);
  if (is_array($d) && count($d) == 3) {
    if (checkdate($d[1], $d[0], $d[2])) {
      $output = "$d[2]-$d[1]-$d[0]";
    }
  }
  return $output;
}

Lets say that you have a date input as $_POST['date'], the function usage is going to be as in below:

$newDate = mysqlDate($_POST['date']);

Just store the $newDate in your database. Works like magic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.