开发者

PHP - Convert two sets of numbers (hr and min) into time

In php is there a way to turn two integers (开发者_如何转开发09 and 30) into into type time (9:30). I need this function so I can INSERT these number into a column in MySQL of type time.


setTime

$datetime = new DateTime(); 
$datetime->setTime(9, 30, 0); 


DateTime::format ( string $format )

will do

$str = '09:' +'30';
$date = DateTime($str);
$date->format('H:i')


In php is there a way to turn two integers (09 and 30) into into type time (9:30). I need this function so I can INSERT these number into a column in MySQL of type time.

Yes, by formating them as string:

$hr  = 9;
$min = 30;

$time = sprintf('%02d:%02d', $hr, $min);

echo $time; # 09:30


As hakre said, use sprintf.

$hours = 9;
$minutes = 30;

$time = sprintf("%02d:%02d:00", $hours, $minutes);


$a1 = array("09","30");
$date implode(":",$a1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜