0

I have a datepicker that shows the date in the following format: date('d/m/Y') the date is represented as: 29/06/2017.

The date column in my database accepts the format: Y-m-d. Witch is represented as: 2017-06-29.

I am trying to convert the date with the following script:

$single_cal4 = date("Y-m-d",strtotime($single_cal4));

When I execute the PHP script that sends the date to the database the date will not insert. I get the date 1970-01-01 in my database.

Does someone know why this is happening and what I am doing wrong?

John
  • 904
  • 8
  • 22
  • 56
  • What does `strtotime($single_cal4)` outputs ? – Seblor Jun 29 '17 at 14:53
  • Standard format for `/`-separated date is `m/d/Y` – u_mulder Jun 29 '17 at 14:54
  • The output is: `-3600` – John Jun 29 '17 at 14:57
  • Append an `$single_cal4 = replace("/" ,"-", $single_cal4);` to your line – Richard Jun 29 '17 at 14:57
  • 1
    Please read [The 3rd NOTE in the manual for `strtotime()`](http://php.net/manual/en/function.strtotime.php) **The manual** is always a good place to START your debugging process – RiggsFolly Jun 29 '17 at 14:57
  • When I do an `echo $single_cal4;` I get: 1970/01/01 – John Jun 29 '17 at 15:01
  • When `strtotime()` see's a slash seperator it assume American date format. So when it tries to work out Month 29 it gives up and outputs the start of the unix clock which is what you got. Convert the slashes to hyphens and it will assume a Sensible date format and then it will work – RiggsFolly Jun 29 '17 at 15:05

1 Answers1

3

Maybe reason is $single_cal4. You make sure that $single_cal4 is valid date string. In other way you can try this:

$myDateTime = DateTime::createFromFormat('d/m/Y', $single_cal4);
$newDateString = $myDateTime->format('Y-m-d');
Sanan Guliyev
  • 711
  • 5
  • 11