0

In my site, I have a bootstrap datepicker which allows user to pick date in format of MM/DD/YYYY (e.g: 05/12/2014). Then when this data is submitted, I used the following PHP code to convert it into Datetime type, then insert into start_date (DATETIME datatype) column in MySQL .

$start_date = date('Y-m-d', $_POST['start_date']);

the insert query in PHP does nothing with reformatting the date. It just simply insert into corresponding column.

However, instead of inserting '2014-05-12', the value inserted into database is '1970-01-01'. That's so weird to me. Can anybody tell me what's wrong here. Is this that I used incorrect PHP function or incorrect timezone setting or ...

3 Answers 3

2

Just do this:

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

2 Comments

Thanks it works correctly. Perhaps, next time I will google more before asking such silly questions like this
Of course, but stackoverflow only allows me to accept correct answer in few minutes later (about 5 mins to go)
1

You could also use strtotime() on your $_POST.

$start_date = date('Y-m-d', strtotime('05/12/2014'));

Comments

1

try to use

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

For more :- Converting between illogically formatted dates (changing /slash/ to -dash- )

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.