0

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 ?

rn605435
  • 175
  • 2
  • 12

3 Answers3

4

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

mdeora
  • 4,152
  • 2
  • 19
  • 29
3

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'
ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44
1
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").

Jack Skeletron
  • 1,351
  • 13
  • 37
  • I tested it, it works as well, I like the fact that you made it with a function, I'm gonna save it, it will probably gonna be useful later, thank you! – rn605435 May 28 '18 at 13:30