如何将字符串" [日期] [时间]转换为[时间]" PHP中的两个日期时间

时间:2016-12-22 22:46:06

标签: php datetime

给定一个总是与这样的模式相匹配的字符串

07-Dec-2016 5:15PM to 5:30PM

使用PHP为格式为yyyy-mm-dd hh:mm-ss的MySQL创建两个日期时间的最简洁方法是什么?

让我自己的设备,我会用substr建立一些笨拙的东西,但我相信PHP可以直观日期?

理想情况下:

function convertTimeGiven($timeString) {
     // some great code

     return $times;
}

echo convertTimeGiven($timeString)['start']; //output 2016-12-07 17:15:00
echo convertTimeGiven($timeString)['end'];   //output 2016-12-07 17:30:00

3 个答案:

答案 0 :(得分:2)

我会这样做:

<?php
    header("Content-type: text/plain");
    $dateTime = "07-Dec-2016 5:15PM to 5:30PM";
    $dateTime = str_replace(" to ", " ", $dateTime);
    $dateTime = explode(" ", $dateTime);
    print_r($dateTime);
    $date1 = strtotime("{$dateTime[0]} {$dateTime[1]}");
    $date2 = strtotime("{$dateTime[0]} {$dateTime[2]}");
    print_r(date("Y-m-d H:i:s", $date1));
    print_r(date("Y-m-d H:i:s", $date2));
?>

<强>输出

Array
(
    [0] => 07-Dec-2016
    [1] => 5:15PM
    [2] => 5:30PM
)
2016-12-07 5:15 pm
2016-12-07 5:30 pm

功能表示

对于你的功能:

<?php
    function convertTimeGiven($dateTime) {
        $dateTime = str_replace(" to ", " ", $dateTime);
        $dateTime = explode(" ", $dateTime);
        $date1 = strtotime("{$dateTime[0]} {$dateTime[1]}");
        $date2 = strtotime("{$dateTime[0]} {$dateTime[2]}");
        return array(
            "start" => date("Y-m-d H:i:s", $date1),
            "end" => date("Y-m-d H:i:s", $date2)
        );
    }
?>

<强>输出

<?php
    header("Content-type: text/plain");
    $dateTime = "07-Dec-2016 5:15PM to 5:30PM";
    function convertTimeGiven($dateTime) {
        $dateTime = str_replace(" to ", " ", $dateTime);
        $dateTime = explode(" ", $dateTime);
        $date1 = strtotime("{$dateTime[0]} {$dateTime[1]}");
        $date2 = strtotime("{$dateTime[0]} {$dateTime[2]}");
        return array(
            "start" => date("Y-m-d H:i:s", $date1),
            "end" => date("Y-m-d H:i:s", $date2)
        );
    }
    print_r(convertTimeGiven($dateTime));
?>
Array
(
    [start] => 2016-12-07 5:15 pm
    [end] => 2016-12-07 5:30 pm
)

参与两天

注意:如果活动在上午12点结束,您需要手动添加一天。

<?php
    header("Content-type: text/plain");
    $dateTime = "07-Dec-2016 11:15PM to 12:30AM";
    function convertTimeGiven($dateTime) {
        $dateTime = str_replace(" to ", " ", $dateTime);
        $dateTime = explode(" ", $dateTime);
        $date1 = strtotime("{$dateTime[0]} {$dateTime[1]}");
        $date2 = strtotime("{$dateTime[0]} {$dateTime[2]}");
        if ($date1 > $date2)
            $date2 = strtotime("{$dateTime[0]} {$dateTime[2]} + 1 day");
        return array(
            "start" => date("Y-m-d H:i:s", $date1),
            "end" => date("Y-m-d H:i:s", $date2)
        );
    }
    print_r(convertTimeGiven($dateTime));
?>

答案 1 :(得分:0)

我想会这样做:

function convertTimeGiven($timeString) {
     $date_start = substr($timeString, 0, 18);       
     $data_end = substr($timeString, 0, 12) . substr($timeString, -7);

     $time_start = strtotime($date_start);
     $time_end = strtotime($data_end);

     if($time_start > $time_end)
     {
        $time_end = strtotime("+1 day", $time_end);
     }

     $times = array(
        'start' => date('Y-m-d H:i:s', $time_start),
        'end' => date('Y-m-d H:i:s', $time_end)
     );

     return $times;
}

它按照你的预期在军事时间返回。

Array ( [start] => 2016-12-07 17:15:00 [end] => 2016-12-07 17:30:00 )

07-Dec-2016 5:15PM to 5:30PM

Array ( [start] => 2016-12-07 17:15:00 [end] => 2016-12-08 00:30:00 )

07-Dec-2016 5:15PM to 12:30AM

答案 2 :(得分:0)

这是我能得到的最干净的事情:)

<?php
function convertTimeGiven($timeString) {
    $inputFormat = 'd-M-Y H:iA';
    $outputFormat = 'Y-m-d H:i:s';
    $exploded = explode(' to', $timeString);
    $start = \DateTime::createFromFormat($inputFormat, $exploded[0]);
    $end = \DateTime::createFromFormat($inputFormat, $start->format('d-M-Y') . $exploded[1]);
    return [
        'start' => $start->format($outputFormat),
        'end' => $end->format($outputFormat)
    ];
}

注意但是,“第二天”的问题仍然存在。 Praveen Kumar发布的答案处理“次日问题”仅适用于时间跨度为'AM'的情况。

如果您输入07-Dec-2016 5:15PM to 4:30PM07-Dec-2016 5:15PM to 6:30PM之类的内容,我们无法确定发生的时间。