将时间分成3个变量

时间:2011-06-16 21:23:11

标签: php datetime

我有时间“早上7点”,我需要分成$hour$min$ampm。有没有一种简单的方法可以将其拆分为3个变量?

5 个答案:

答案 0 :(得分:6)

$a      = preg_split('/[: ]/', '7:00 am');
$hour   = $a[0][0];
$minute = $a[1][0];
$ampm   = $a[2][0];

答案 1 :(得分:3)

sscanf( "7:00 am", "%d:%d %s", $hour, $min, $ampm );

答案 2 :(得分:2)

$yourtime = "7:00 am";
preg_match("/([0-9]{1,2}):([0-9]{1,2}) ([a-zA-Z]+)/", $yourtime, $match);
$hour = $match[1];
$min = $match[2];
$ampm = $match[3];

答案 3 :(得分:2)

$time = '7:00 am';
list ($time, $ampm) = explode(' ', $time);
list ($hour, $min) = explode(':', $time);

答案 4 :(得分:1)

$time = '7:00 am';
$hour = date("g",$time);
$min = date("i",$time);
$ampm = date("a",$time);

http://php.net/manual/function.date.php