2

Hello i am trying to convert this date.

01. 06. 2015

Into mysql format, so i can insert it into database.

I tried this code, and other ombination with date and date format function.

`$mysqldate = date( 'Y-m-d H:i:s', strtotime($datefrom));`

but i get result

1.1.1970

Is it possible to do this with some date function, or i must use regex for resolving this problem. Maybe dateformat function?

  • http://www.w3schools.com/sql/func_date_format.asp – odedta Jun 29 '15 at 07:31
  • You could explode it, trim it and then use `mktime()` to generate a valid date. As it is it is not a format that is recognized by `strtotime()`. You could also generate a DateTime object from a specific format but then the format would have to be exactly fixed. – jeroen Jun 29 '15 at 07:31

2 Answers2

3

Use this.. works fine

$datefrom = "01. 06. 2015";
$datefrom = str_replace( ". ", "-", $datefrom);
$mysqldate = date( 'Y-m-d H:i:s', strtotime($datefrom));
syed suleman
  • 542
  • 2
  • 6
2

you need to clean your dateformat

  <?php
     $date = '01. 06. 2015';
     $date = str_replace(' ', '', $date); // replace whitespaces
     $mysqldate = date( 'Y-m-d H:i:s', strtotime($date));
donald123
  • 5,638
  • 3
  • 26
  • 23