Convert String To date in PHP

后端 未结 9 2027
清酒与你
清酒与你 2020-12-15 08:54

How can I convert this string 05/Feb/2010:14:00:01 to unixtime ?

相关标签:
9条回答
  • 2020-12-15 09:18
    $d="05/Feb/2010:14:00:01";
    $dr= date_create_from_format('d/M/Y:H:i:s', $d);
    echo $dr->format('Y-m-d H:i:s');
    

    here you get date string, give format specifier in ->format() according to format needed

    0 讨论(0)
  • 2020-12-15 09:21

    Use the strtotime function:

    Example:

     $date = "25 december 2009";
     $my_date = date('m/d/y', strtotime($date));
     echo $my_date;
    
    0 讨论(0)
  • 2020-12-15 09:22

    If it's a string that you trust meaning that you have checked it before hand then the following would also work.

    $date = new DateTime('2015-03-27');
    
    0 讨论(0)
  • 2020-12-15 09:24

    http://www.php.net/date_parse_from_format

    0 讨论(0)
  • 2020-12-15 09:26

    You should look into the strtotime() function.

    0 讨论(0)
  • 2020-12-15 09:30

    Simple exploding should do the trick:

    $monthNamesToInt = array('Jan'=>1,'Feb'=>2, 'Mar'=>3 /*, [...]*/ );
    $datetime = '05/Feb/2010:14:00:01';
    list($date,$hour,$minute,$second) = explode(':',$datetime);
    list($day,$month,$year) = explode('/',$date);
    
    $unixtime = mktime((int)$hour, (int)$minute, (int)$second, $monthNamesToInt[$month], (int)$day, (int)$year);
    
    0 讨论(0)
提交回复
热议问题