开发者

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

开发者 https://www.devze.com 2023-03-23 13:02 出处:网络
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

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

精彩评论

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

关注公众号