1

I have a problem with this code, when I try to use the dateformat dd/mm/yy it saves 1970/01/01 but when I use mm/dd/yy it works, I really dont know why here is the code I'm using:

HTML

<input type="text" name="dateStart" class="datepicker">

JS

$( ".datepicker" ).datepicker({ dateFormat: "mm/dd/yy" }); (Works)
$( ".datepicker" ).datepicker({ dateFormat: "dd/mm/yy" }); (Does not)

PHP

$date = isset($_POST['dateStart'])?date("Y-m-d H:i:s",strtotime($_POST['dateStart']));

And the mysql column is a date field.

Any help would be appreciated.

UPDATE

-It´s a mysql datetime field

2
  • mysql date format should be Y-m-d Commented May 24, 2017 at 4:32
  • 1
    Can you please let us know what is your table field type? i mean is it date or datetimeor timestamps? Commented May 24, 2017 at 4:33

6 Answers 6

1

1) You can show convenient Format to user end But when you store it in database Mysql date Format should be 2017-05-24 'YYYY-MM-DD'

JS

$( ".datepicker" ).datepicker({ dateFormat: "dd/mm/yy" });

2) And strtotime will not accept this date format 24/05/2017 seperated by slash so use string replace. and then change the format like this

PHP

$date = str_replace('/', '-', $_POST['dateStart']);
$date = date('Y-m-d', strtotime($date));
Sign up to request clarification or add additional context in comments.

Comments

1

You can use datepicker to show your users date in the desired format as you already have:

<form>
   <input type="text" name="dateStart" class="datepicker">
</form>
<script>
  $(".datepicker").datepicker({ dateFormat: "dd/mm/yy" });
</script>

Then when you submit to PHP, you convert it so that you can store it correctly in the database:

<?php 

$date = $_POST['dateStart'];
$date_for_database = date('Y-m-d', strtotime($date));
var_dump($date_for_database);

7 Comments

Strtotime() doesn't work with dd/mm/YYYY format .i mean separated by slash format will not work .
@JYoThI I disagree. strtotime('08/23/1994')works. What makes you think it does not?
can you try this example echo date('Y-m-d',strtotime('24/05/2017')); @CodeGodie
@JYoThI That wont work as 24/05/2017 is incorrect. It should be 05/24/2017 as they originally need.
the how it will work for this format "dd/mm/yy" as you suggested
|
0

You have to try this

$date = str_replace('/', '-', $_POST['dateStart']);
$date = date('Y-m-d H:i:s', strtotime($date));

Comments

0

No need to reformat the date just post like what you want to save.

//mysql standard date format is: year-month-day
$( ".datepicker" ).datepicker({ dateFormat: "yy-mm-dd" });

Comments

0

This might work

$date = isset($_POST['dateStart'])?date("m j Y",strtotime($_POST['dateStart']));

Comments

0

Read Stortime manual.

Note:

m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

$date = str_replace('/', '-', $_POST['dateStart']);
echo date('Y-m-d', strtotime($date));

Strtotime() doesn't work with dd/mm/YYYY format

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.