how to re-format datetime string in php?
Solution 1
why not use date() just like below,try this
$t = strtotime('20130409163705');
echo date('d/m/y H:i:s',$t);
and will be output
09/04/13 16:37:05
Solution 2
For 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
Solution 3
date("Y-m-d H:i:s", strtotime("2019-05-13"))
Solution 4
If you want to use substr()
, you can easily add the dashes or slashes like this..
$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.
Solution 5
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
Psychocryo
Updated on July 09, 2022Comments
-
Psychocryo almost 2 years
I receive a datetime from a plugin. I put it into a variable:
$datetime = "20130409163705";
That actually translates to
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
.How do I go about and change the orders of the string?
As for now I could think is to use substr to separate the date and time. I'm still not sure on how to add the additional characters and rearrange the date.
-
Jesse about 11 yearsHi, welcome to Stack Overflow! A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it's there. Always quote the most relevant part of an important link. Imagine that page is moved to another server, or the direct link changes - future users will not be able to benefit from the answer. Please take a look at how to answer.
-
Nico Haase almost 5 yearsCan you explain further how that given line relates to the question?
-
DaFois almost 5 yearsPlease add some explanation for future users to your answer