how to re-format datetime string in php?

后端 未结 8 924
栀梦
栀梦 2020-11-29 07:00

I receive a datetime from a plugin. I put it into a variable:

$datetime = \"20130409163705\"; 

That actually translates to yyyymm

相关标签:
8条回答
  • 2020-11-29 07:30

    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
    
    0 讨论(0)
  • 2020-11-29 07:31

    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
    
    0 讨论(0)
  • 2020-11-29 07:37

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

    0 讨论(0)
  • 2020-11-29 07:48

    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
    
    0 讨论(0)
  • 2020-11-29 07:49

    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.

    0 讨论(0)
  • 2020-11-29 07:49

    You can use date_parse_from_format() function ...

    Check this link..you will get clear idea

    0 讨论(0)
提交回复
热议问题