将时间转换为PHP中的整数

Dan*_*nga 1 php

您如何将时间转换为整数?

 string(8) "04:04:07"
Run Code Online (Sandbox Code Playgroud)

我希望这是4(小时)或更佳的4.04(4小时4分钟)

我试过了

  $yourdatetime = "04:04:07";
  $timestamp = strtotime($yourdatetime);
Run Code Online (Sandbox Code Playgroud)

导致

 int(1458590887)
Run Code Online (Sandbox Code Playgroud)

ran*_*ame 5

日期的功能是你的朋友:

鉴于您上面的代码:

$yourdatetime = "04:04:07";
$timestamp = strtotime($yourdatetime);
Run Code Online (Sandbox Code Playgroud)

然后可以将其输入到date函数中:

echo 'Hours:' . date('h', $timestamp);  // Hours: 04
echo 'Minutes:' . date('i', $timestamp); // Minutes: 04
echo 'Seconds:' . date('s', $timestamp); // Seconds: 07
Run Code Online (Sandbox Code Playgroud)

请参考文档,了解您想要使用几个小时的特定格式-有很多选择。

您甚至可以采取以下行动:

echo date('h.i', $timestamp); // 04.04
Run Code Online (Sandbox Code Playgroud)

如果需要真正的数字:

echo float(date('h.i', $timestamp)); // 4.04
Run Code Online (Sandbox Code Playgroud)