how to re-format datetime string in php?

PhpDateSortingTimeDate Sorting

Php Problem Overview


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 or 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 substr to separate the date and time. I'm still not sure on how to add the additional characters and rearrange the date.

Php Solutions


Solution 1 - Php

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 - Php

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 - Php

date("Y-m-d H:i:s", strtotime("2019-05-13"))

Solution 4 - Php

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 - Php

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

Solution 6 - Php

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.

Solution 7 - Php

https://en.functions-online.com/date.html?command={"format":"l jS \\of F Y h:i:s A"}

Solution 8 - Php

You have different options.

Using date():

$format = date('d/m/y H:i:s', 1621371929);
echo $format;

the output is:

18/05/21 14:05:29

Using date_format():

$date = date_create(1621371929);
echo date_format($date, 'd/m/y H:i:s');

the output is:

18/05/21 14:05:29

Using DateTime::format():

$date = new DateTime('2021-05-18 14:05:29');
echo $date->format('d/m/y H:i:s');

the output is:

18/05/21 14:05:29

This article gives you an overview of these three methods, and this one is DateTime::format() in depth.

Solution 9 - Php

You can use date_parse_from_format() function ...

date_parse_from_format(string $format, string $datetime): array
Example
<?php
$date = "6.1.2009 13:00+01:00";
print_r(date_parse_from_format("j.n.Y H:iP", $date));
?>

The above example will output:

Array
(
    [year] => 2009
    [month] => 1
    [day] => 6
    [hour] => 13
    [minute] => 0
    [second] => 0
    [fraction] => 
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

    [is_localtime] => 1
    [zone_type] => 1
    [zone] => 3600
    [is_dst] => 
)

Check the PHP docs..you will get clear idea

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPsychocryoView Question on Stackoverflow
Solution 1 - PhpbluewindView Answer on Stackoverflow
Solution 2 - PhpAndrey VolkView Answer on Stackoverflow
Solution 3 - PhpChuanyi LiuView Answer on Stackoverflow
Solution 4 - PhpI wrestled a bear once.View Answer on Stackoverflow
Solution 5 - PhpSuresh KamrushiView Answer on Stackoverflow
Solution 6 - PhpmishmashView Answer on Stackoverflow
Solution 7 - PhpskpaikView Answer on Stackoverflow
Solution 8 - PhpDomenico RuggianoView Answer on Stackoverflow
Solution 9 - PhpSeliCView Answer on Stackoverflow