Php 从到小时获得分钟的最佳方式?
例如,我有:Php 从到小时获得分钟的最佳方式?,php,Php,例如,我有: $hours = "10:11 - 13:34"; $hours = "10:11 - 13:34"; // Extract times from string list($time1, $time2) = explode('-', $hours); // Remove white-space $time1 = trim($time1); $time2 = trim($time2); // Format as minutes, and add into an array $
$hours = "10:11 - 13:34";
$hours = "10:11 - 13:34";
// Extract times from string
list($time1, $time2) = explode('-', $hours);
// Remove white-space
$time1 = trim($time1);
$time2 = trim($time2);
// Format as minutes, and add into an array
$result = [
DateTime::createFromFormat('H:i', $time1)->format('i'),
DateTime::createFromFormat('H:i', $time2)->format('i'),
];
print_r($result);
使用此字符串获取分钟的最佳方式是什么?例如,我最希望将它们放在数组中
$results = array(11, 34);
然后也可以是:
$hours = "7:10 - 22:00";
$hours = "07:55-14:15";
$hours = "07:55 -14:15";
等等
我可以:
$expl = explode('-', trim($hours));
接下来是“:”,但也许是更好的方式?我认为,赛前准备就是你们所需要的
$a = "7:10 - 22:00";
preg_match_all("/[0-9]+:(?P<minutes>[0-9]+)/", $a, $matches);
$minutes = array_map(
function($i) {
return intval($i);
},
$matches["minutes"]
);
var_dump($minutes);
您可以使用PHP的
DateTime
类来操作日期和时间,而不是使用字符串操作
$time = '10:11';
$minutes = DateTime::createFromFormat('H:i', $time)->format('i');
echo $minutes; // 11
以你为例:
$hours = "10:11 - 13:34";
$hours = "10:11 - 13:34";
// Extract times from string
list($time1, $time2) = explode('-', $hours);
// Remove white-space
$time1 = trim($time1);
$time2 = trim($time2);
// Format as minutes, and add into an array
$result = [
DateTime::createFromFormat('H:i', $time1)->format('i'),
DateTime::createFromFormat('H:i', $time2)->format('i'),
];
print_r($result);
=
利用时间!它真的很强大
<?php
function getMinuteFromTimes($time){
//strip all white not just spaces
$tmp = preg_replace('/\s+/', '', $time);
//explode the time
$tmp = explode ('-',$tmp);
//make our return array
$return = array();
//loop our temp array
foreach($tmp as $t){
//add our minutes to the new array
$return[] = date('i',strtotime($t));
}
//return the array
return $return;
}
print_r(getMinuteFromTimes("7:10 - 22:00"));
echo '</br>';
print_r(getMinuteFromTimes("07:55-14:15"));
echo '</br>';
print_r(getMinuteFromTimes("07:55 -14:15"));
?>
输出:数组([0]=>10[1]=>00)
数组([0]=>55[1]=>15)
数组([0]=>55[1]=>15)
此函数还将接受诸如“12:00-13:13-15:12”之类的输入或单个输入。Do it regexp方式用于生成(研究)正则表达式和代码。