I have a time format from mysql who looks like this : 07:22:46.5714
And I want to display instead 07h23min
I have searched php already existing function for this but it seems like I have to wrote one, how can I do this ?
I have a time format from mysql who looks like this : 07:22:46.5714
And I want to display instead 07h23min
I have searched php already existing function for this but it seems like I have to wrote one, how can I do this ?
You can do this easily. Check the below code.
$time = strtotime('07:22:46.5714');
$display = Date('h\hi\m\i\n',$time);
var_dump($display);
Output is - 07h22min
You have two options :
you can format the time using MySQL function DATE_FORMAT()
like this :
SELECT DATE_FORMAT(mydate, '%kh%imin') FROM table
The other solution is to use php function explode()
like this :
$timeParts = explode(':', '07:22:46.5714');
$timeString = $timeParts[0] . 'h' . $time[1] . 'min'
function myPersonalDateFormat($time){
$t = strtotime($time);
return sprintf("%sh%smin", date("H", $t), date("i", $t));
}
// This will print 07h22min
echo myPersonalDateFormat('07:22:46.5714');
This is using a combination strtotime, that converts your string to a timestamp, date, that allows to print a given timestamp to a specifide format (so you can get only the hours part or minutes, and sprintf that returns a string according to the given format %sh%smin
(%s is a placeholder for "string").