如何将MM / DD / YYYY转换成YYYY-MM-DD?

我有一个jquery日历,将输入值设置为MM / DD / YYYY

我如何转换它,以便我的数据库列(日期)可以正确接受它?

编辑

戈登是对的 – 他的链接指向了我这个答案

$mysql_date = date('Ymd H:i:s', strtotime($user_date)); 

 $date = "07/12/2010"; $your_date = date("Ymd", strtotime($date)); 

我希望我的回答很有用:)

你想用PHP做到这一点,对吗?

  1. 使用爆炸

     $christmas = "12/25/2010"; $parts = explode('/',$christmas); $yyyy_mm_dd = $parts[2] . '-' . $parts[0] . '-' . $parts[1] 
  2. 使用strptime将其解析为时间戳,然后使用strftime将其格式化为您想要的格式。

请尝试以下代码,

 format('Ym-d'); ?> 

我们需要更多信息?

1)什么脚本插入数据库? 我假设PHP

2)我们还需要知道你如何存储日期和格式?

我的解决方案是:

 $dates = preg_split('/\//',$_POST['date']); $month = $dates[0]; $day = $dates[1]; $year = $dates[2]; $finalDate = $year.'-'.$month.'-'.$day; 

试试这个:

 $date = explode('/', '16/06/2015'); $new = date('Ymd H:i:s', strtotime(implode('-', array_reverse($date)))); 

返回:2015-06-15 00:00:00

希望这可以帮助!

或者,您可以在不使用explode情况下执行此操作:

 $date = "12/25/2010"; $mysql_date = date('Ym-d', strtotime($date)); 

您现在可以使用$mysql_date插入数据库。

我建议使用strtotime()函数,然后使用date() 。 通过这个你可以转换任何格式的日期。

 $unix_time_stamp = strtotime($mm_dd_yyyy); $yyyy_mm_dd = date(Y/m/d,$unix_timestamp); 

如果你只想要一行试试这个:

 $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); 

将日期值分配给变量$d

$dob表示您希望转换的日期

 $d= date('Ym-d',strtotime ($dob) ); 

请尝试以下代码:

 $date = "01-14-2010"; echo $your_date = date("Ymd", strtotime($date)); 

它将回应1969-12-31