I have a jquery calendar that sets the input value to MM/DD/YYYY
How would I convert it so that my database column (date) can accept it correctly?
EDIT开发者_高级运维
Gordon was right - his link pointed me to this answer
$mysql_date = date('Y-m-d H:i:s', strtotime($user_date));
$date = "07/12/2010";
$your_date = date("Y-m-d", strtotime($date));
I hope my answer is useful :)
You want to do this in PHP, right?
Use Explode
$christmas = "12/25/2010"; $parts = explode('/',$christmas); $yyyy_mm_dd = $parts[2] . '-' . $parts[0] . '-' . $parts[1]
Use
strptime
to parse it to a timestamp and thenstrftime
to format it the way you want it.
Try the following code,
<?php
$date = "10/24/2014";
$date = DateTime::createFromFormat("m/d/Y" , $date);
echo $date->format('Y-m-d');
?>
Try this:
$date = explode('/', '16/06/2015');
$new = date('Y-m-d H:i:s', strtotime(implode('-', array_reverse($date))));
Returns: 2015-06-15 00:00:00
Hope this helps!
We need more information?
1) What script is inserting into database? I am assuming PHP
2) Also we need to know how you are storing the date and in what format?
My solution would be:
$dates = preg_split('/\//',$_POST['date']);
$month = $dates[0];
$day = $dates[1];
$year = $dates[2];
$finalDate = $year.'-'.$month.'-'.$day;
Alternatively you can do this without using the explode
:
$date = "12/25/2010";
$mysql_date = date('Y-m-d', strtotime($date));
You can now use $mysql_date
to insert into your database.
I will suggest to use strtotime()
function and then date()
. By this you can convert any format of date.
$unix_time_stamp = strtotime($mm_dd_yyyy);
$yyyy_mm_dd = date(Y/m/d,$unix_timestamp);
if you just want one line try this:
$time = "07/12/2010";
$new_time = preg_replace("!([01][0-9])/([0-9]{2})/([0-9]{4})!", "$3-$1-$2", $time);
var_dump($time);
var_dump($new_time);
精彩评论