php 如何在php中重新格式化日期时间字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15920768/
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
how to re-format datetime string in php?
提问by Psychocryo
I receive a datetime from a plugin. I put it into a variable:
我从插件收到日期时间。我把它放到一个变量中:
$datetime = "20130409163705";
That actually translates to yyyymmddHHmmss
.
这实际上转化为yyyymmddHHmmss
.
I would need to display this to the user as a transaction time but it doesn't look proper.
我需要将此作为交易时间显示给用户,但它看起来不正确。
I would like to arrange it to be like 09/04/2013 16:37:05
or09-apr-2013 16:37:05
.
我想把它安排成像或。09/04/2013 16:37:05
09-apr-2013 16:37:05
How do I go about and change the orders of the string?
如何更改字符串的顺序?
As for now I could think is to use substrto separate the date and time. I'm still not sure on how to add the additional characters and rearrange the date.
至于现在我能想到的是使用substr来分隔日期和时间。我仍然不确定如何添加其他字符并重新排列日期。
回答by bluewind
why not use date()just like below,try this
为什么不使用date()就像下面一样,试试这个
$t = strtotime('20130409163705');
echo date('d/m/y H:i:s',$t);
and will be output
并将输出
09/04/13 16:37:05
回答by Andrey Volk
For PHP 5 >= 5.3.0 http://www.php.net/manual/en/datetime.createfromformat.php
对于 PHP 5 >= 5.3.0 http://www.php.net/manual/en/datetime.createfromformat.php
$datetime = "20130409163705";
$d = DateTime::createFromFormat("YmdHis", $datetime);
echo $d->format("d/m/Y H:i:s"); // or any you want
Result:
结果:
09/04/2013 16:37:05
回答by I wrestled a bear once.
If you want to use substr()
, you can easily add the dashes or slashes like this..
如果你想使用substr()
,你可以很容易地添加这样的破折号或斜线..
$datetime = "20130409163705";
$yyyy = substr($datetime,0,4);
$mm = substr($datetime,4,6);
$dd = substr($datetime,6,8);
$hh = substr($datetime,8,10);
$MM = substr($datetime,10,12);
$ss = substr($datetime,12,14);
$dt_formatted = $mm."/".$dd."/".$yyyy." ".$hh.":".$MM.":".$ss;
You can figure out any further formatting from that point.
您可以从该点找出任何进一步的格式。
回答by Suresh Kamrushi
try this
尝试这个
$datetime = "20130409163705";
print_r(date_parse_from_format("Y-m-d H-i-s", $datetime));
the output:
输出:
[year] => 2013
[month] => 4
[day] => 9
[hour] => 16
[minute] => 37
[second] => 5
回答by vanneto
You could do it like this:
你可以这样做:
<?php
$datetime = "20130409163705";
$format = "YmdHis";
$date = date_parse_from_format ($format, $datetime);
print_r ($date);
?>
You can look at date_parse_from_format()
and the accepted format values.
回答by SeliC
回答by skpaik
https://en.functions-online.com/date.html?command={"format":"l jS \of F Y h:i:s A"}
回答by Chuanyi Liu
date("Y-m-d H:i:s", strtotime("2019-05-13"))
date("Y-m-d H:i:s", strtotime("2019-05-13"))