将日期范围转换为时间戳

时间:2014-03-16 12:41:20

标签: php

$time_range_from_user = '2014-03-28 17:00:00 to 2014-03-28 18:00:00';

有什么方法可以将$ time_range_from_user转换为时间戳并分别返回开始时间和结束时间?

2 个答案:

答案 0 :(得分:1)

使用 explode() 将选定分隔符上的字符串分隔为数组。在这种情况下,我们将使用分隔符'到'分开字符串。

$time_range_from_user = '2014-03-28 17:00:00 to 2014-03-28 18:00:00';

$ranges = explode(' to ', $time_range_from_user); //Split string on ' to '

$start_timestamp = strtotime($ranges[0]); //Take first result and turn it into a timestamp

$end_timestamp = strtotime($ranges[1]); //Take second result and turn it into a timestamp

echo date('Y-m-d H:i:s', $start_timestamp) . ' ' . date('Y-m-d H:i:s', $end_timestamp); //Output both time stamps in Y-m-d H:i:s format.

变量$start_timestamp$end_timestamp分别包含2014-03-28 17:00:002014-03-28 18:00:00表示的UNIX时间戳。最后,我echo使用date()格式化输出以匹配您的初始格式。

答案 1 :(得分:0)

em ...将它拆分为'到'串? $parts = explode(' to ', $time_range_from_user);

相关问题