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?

7
  • What does strtotime($single_cal4) outputs ? Commented Jun 29, 2017 at 14:53
  • Standard format for /-separated date is m/d/Y Commented Jun 29, 2017 at 14:54
  • The output is: -3600 Commented Jun 29, 2017 at 14:57
  • Append an $single_cal4 = replace("/" ,"-", $single_cal4); to your line Commented Jun 29, 2017 at 14:57
  • 1
    Please read The 3rd NOTE in the manual for strtotime() The manual is always a good place to START your debugging process Commented Jun 29, 2017 at 14:57

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

3 Comments

I get an error with your script
Can you share error?
My mistake. It is working. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.