Reputation: 27
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
Upvotes: 1
Views: 1765
Reputation: 12085
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));
Upvotes: 1
Reputation: 6650
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
Upvotes: 0
Reputation: 61
This might work
$date = isset($_POST['dateStart'])?date("m j Y",strtotime($_POST['dateStart']));
Upvotes: 0
Reputation: 12132
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);
Upvotes: 1
Reputation: 2036
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" });
Upvotes: 0
Reputation: 449
You have to try this
$date = str_replace('/', '-', $_POST['dateStart']);
$date = date('Y-m-d H:i:s', strtotime($date));
Upvotes: 0