php 如何将 MM/DD/YYYY 转换为 YYYY-MM-DD?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3288257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:14:32  来源:igfitidea点击:

How to convert MM/DD/YYYY to YYYY-MM-DD?

phpjquerydate

提问by st4ck0v3rfl0w

I have a jquery calendar that sets the input value to MM/DD/YYYY

我有一个 jquery 日历,将输入值设置为 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));

回答by Dae KIM

$date = "07/12/2010";
$your_date = date("Y-m-d", strtotime($date));

I hope my answer is useful :)

我希望我的回答有用:)

回答by Alex

You want to do this in PHP, right?

您想在 PHP 中执行此操作,对吗?

  1. Use Explode

    $christmas = "12/25/2010";
    $parts = explode('/',$christmas);
    $yyyy_mm_dd = $parts[2] . '-' . $parts[0] . '-' . $parts[1]
    
  2. Use strptimeto parse it to a timestamp and then strftimeto format it the way you want it.

  1. 使用爆炸

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

回答by saravanavelu

Try the following code,

试试下面的代码,

<?php
$date = "10/24/2014";
$date = DateTime::createFromFormat("m/d/Y" , $date);
echo $date->format('Y-m-d');
?>

回答by Gabriel Glauber

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

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

Hope this helps!

希望这可以帮助!

回答by Lizard

We need more information?

我们需要更多信息?

1) What script is inserting into database? I am assuming PHP

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

2) Also we need to know how you are storing the date and in what format?

2)我们还需要知道您如何存储日期以及以什么格式存储?

My solution would be:

我的解决方案是:

$dates = preg_split('/\//',$_POST['date']);

$month = $dates[0];
$day = $dates[1];
$year = $dates[2];

$finalDate = $year.'-'.$month.'-'.$day;

回答by Shubham

I will suggest to use strtotime()function and then date(). By this you can convert any format of date.

我会建议使用strtotime()function 然后date(). 通过这种方式,您可以转换任何格式的日期。

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

回答by Sarfraz

Alternatively you can do this without using the explode:

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

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

You can now use $mysql_dateto insert into your database.

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

回答by wodka

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})!", "--", $time);

var_dump($time);
var_dump($new_time);

回答by Unni Kr

assign the date values to variable $d

将日期值分配给变量 $d

$dobdenotes the date which you wish to convert

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

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

回答by Jean Joel Desire Adome kakou

Try the following code:

试试下面的代码:

$date = "01-14-2010";
echo $your_date = date("Y-m-d", strtotime($date));

It will echo 1969-12-31.

它会回声1969-12-31