PHP 转换日期格式 dd/mm/yyyy => yyyy-mm-dd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10306999/
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
PHP convert date format dd/mm/yyyy => yyyy-mm-dd
提问by Daniel Mabinko
I am trying to convert a date from dd/mm/yyyy => yyyy-mm-dd. I have using the mktime() function and other functions but I cannot seem to make it work. I have managed to explodethe original date using '/'as the delimiter but I have no success changing the format and swapping the '/'with a '-'.
我正在尝试将日期从dd/mm/yyyy => yyyy-mm-dd. 我已经使用了 mktime() 函数和其他函数,但我似乎无法让它工作。我已成功地explode使用原来的日期'/'作为分隔符,但我没有成功改变格式和交换的'/'一个'-'。
Any help will be greatly appreciated.
任何帮助将不胜感激。
回答by hjpotter92
Dates in the
m/d/yord-m-yformats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the Americanm/d/yis assumed; whereas if the separator is a dash (-) or a dot (.), then the Europeand-m-yformat is assumed. Check more here.
m/d/y或d-m-y格式中的日期可以通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜杠 (/),则m/d/y假定是美国人;而如果分隔符是破折号 (-) 或点 (.),则d-m-y假定为欧洲格式。在此处查看更多信息。
Use the default date function.
使用默认日期函数。
$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );
EDITI just tested it, and somehow, PHP doesn't work well with dd/mm/yyyy format. Here's another solution.
编辑我刚刚测试了它,不知何故,PHP 不能很好地处理 dd/mm/yyyy 格式。这是另一个解决方案。
$var = '20/04/2012';
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));
回答by Baba
Try Using DateTime::createFromFormat
尝试使用 DateTime::createFromFormat
$date = DateTime::createFromFormat('d/m/Y', "24/04/2012");
echo $date->format('Y-m-d');
Output
输出
2012-04-24
EDIT:
编辑:
If the date is 5/4/2010 (both D/M/YYYY or DD/MM/YYYY), this below method is used to convert 5/4/2010 to 2010-4-5 (both YYYY-MM-DD or YYYY-M-D) format.
如果日期是 5/4/2010(D/M/YYYY 或 DD/MM/YYYY),则使用以下方法将 5/4/2010 转换为 2010-4-5(YYYY-MM-DD 或YYYY-MD) 格式。
$old_date = explode('/', '5/4/2010');
$new_data = $old_date[2].'-'.$old_date[1].'-'.$old_date[0];
OUTPUT:
输出:
2010-4-5
回答by tosin
Here's another solution not using date(). not so smart:)
这是另一个不使用 date() 的解决方案。没那么聪明:)
$var = '20/04/2012';
echo implode("-", array_reverse(explode("/", $var)));
回答by honyovk
Do this:
做这个:
date('Y-m-d', strtotime('dd/mm/yyyy'));
But make sure 'dd/mm/yyyy' is the actual date.
但请确保 'dd/mm/yyyy' 是实际日期。
回答by Achraf Almouloudi
I can see great answers, so there's no need to repeat here, so I'd like to offer some advice:
我可以看到很好的答案,所以这里没有必要重复,所以我想提供一些建议:
I would recommend using a Unix Timestamp integer instead of a human-readable date format to handle time internally, then use PHP's date()function to convert the timestamp value into a human-readable date format for user display. Here's a crude example of how it should be done:
我建议使用 Unix Timestamp 整数而不是人类可读的日期格式在内部处理时间,然后使用 PHP 的date()函数将时间戳值转换为人类可读的日期格式以供用户显示。这是一个如何完成的粗略示例:
// Get unix timestamp in seconds
$current_time = date();
// Or if you need millisecond precision
// Get unix timestamp in milliseconds
$current_time = microtime(true);
Then use $current_timeas needed in your app (store, add or subtract, etc), then when you need to display the date value it to your users, you can use date()to specify your desired date format:
然后$current_time在您的应用程序中根据需要使用(存储、添加或减去等),然后当您需要向用户显示日期值时,您可以使用date()指定所需的日期格式:
// Display a human-readable date format
echo date('d-m-Y', $current_time);
This way you'll avoid much headache dealing with date formats, conversions and timezones, as your dates will be in a standardized format (Unix Timestamp) that is compact, timezone-independent (always in UTC) and widely supported in programming languages and databases.
通过这种方式,您将避免处理日期格式、转换和时区的麻烦,因为您的日期将采用标准化格式(Unix 时间戳),该格式紧凑、与时区无关(始终采用 UTC)并在编程语言和数据库中得到广泛支持.

