将日期 dd/mm/yyyy 格式化为 yyyy-mm-dd PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15676344/
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
Format date dd/mm/yyyy to yyyy-mm-dd PHP
提问by Darren Sweeney
I have a form within which a date is input in UK format, and I need to convert it to
我有一个以英国格式输入日期的表单,我需要将其转换为
yyyy-mm-dd
e.g. a date entered is: 31/03/2013
which I want to convert to '2013-03-31'
for database insert.
例如,输入的日期是:31/03/2013
我想转换'2013-03-31'
为数据库插入的日期。
I'm using the following which bizarrely works only sometimes:
我正在使用以下内容,但有时会奇怪地起作用:
$dateInput = $mysqli->real_escape_string($_POST['date']);
$show_date = date('Y-m-d', strtotime($dateInput));
Is there a better way to do this?
有一个更好的方法吗?
回答by sroes
You might want to use DateTime::createFromFormat:
您可能想要使用DateTime::createFromFormat:
$show_date = DateTime::createFromFormat('d/m/Y', $dateInput)->format('Y-m-d');
回答by Dieepak
try it.
尝试一下。
$dateInput = explode('/','31/03/2013');
$ukDate = $dateInput[2].'-'.$dateInput[1].'-'.$dateInput[0];
回答by bipen
following which bizarrely works only sometimes:
以下只是有时奇怪地起作用:
$show_date = date('Y-m-d', strtotime($dateInput));
this is no other shorter way to do it.... and i am using this all my life.. havn't notice any bizzare things going on with it till now.. check if there is some other things that is messing up
这不是其他更短的方法来做到这一点......而且我一生都在使用它......直到现在还没有注意到任何奇怪的事情......检查是否还有其他一些事情搞砸了
otherway to do it is
否则这样做是
$timestamp = strtotime(str_replace('/', '.', $dateInput));
$mysql_date = date('Y-m-d', $timestamp);
回答by Arvind
PHP 5.3 and up
PHP 5.3 及更高版本
Use DateTime::createFromFormat.It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.
使用DateTime::createFromFormat。它允许你指定一个精确的掩码——使用 date() 语法——来解析传入的字符串日期。
PHP 5.2 and lower
PHP 5.2 及更低版本
You will have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime()that will build you a timestamp.
您必须使用 substr() 手动解析元素(年、月、日、小时、分钟、秒),并将结果传递给mktime(),后者将为您构建时间戳。
回答by Prasanth Bendra
Try this :
尝试这个 :
$dte = '28/03/2013';
$dt = new DateTime();
$date = $dt->createFromFormat('d/m/Y', $dte);
echo $date->format('Y-m-d');
Output: 2013-03-28
输出: 2013-03-28
回答by Dew Drop
$date_array = explode("/",$your_date); // split the array
$var_day = $date_array[0]; //day seqment
$var_month = $date_array[1]; //month segment
$var_year = $date_array[2]; //year segment
echo $new_date_format = "$var_year-$var_day-$var_month"; // join them together
This will work
这将工作
回答by Absolute?ER?
Couple of alternate methods. Both output 2013-03-31:
几种替代方法。两个输出 2013-03-31:
Method 1 - "Look ma, no functions"
方法一——“你看,没有功能”
<?php
$in = '31/03/2013';
echo $in[6].$in[7].$in[8].$in[9].'-'.$in[3].$in[4].'-'.$in[0].$in[1];
?>
Method 2 - regex
方法 2 - 正则表达式
<?php
echo ($output = preg_replace('!^([0-9]{2})/([0-9]{2})/([0-9]{4})$!',"--",$input));
?>