将时间转换为Php中的整数

How would you convert time to integer?

 string(8) "04:04:07"

I want this as 4(hours) or much better 4.04(4hours and 4 minutes)

I tried

  $yourdatetime = "04:04:07";
  $timestamp = strtotime($yourdatetime);

Which results in

 int(1458590887)

Use (float) and preg_replace to one-line-code conversion:

$floatval = (float) preg_replace('/^(\d+):(\d+).+/','\1.\2',$yourdatetime);

So we'll break this out. If we cast the hours as an integer and leave the minutes as a string this is a pretty simple conversion

$time = explode(':', $yourdatetime);
$hours = (int)$time[0] . '.' . $time[1];

Avoids any overhead from regex

The date function is your friend:

Given your own code above:

$yourdatetime = "04:04:07";
$timestamp = strtotime($yourdatetime);

You can then feed it into the date function:

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

Refer to the docs for the specific format(s) you'd like for hours - there's many options.

You could even do it in one move:

echo date('h.i', $timestamp); // 04.04

If you need it truly numeric:

echo float(date('h.i', $timestamp)); // 4.04

strtotime() returns the time in seconds since the Unix Epoch. You can then format this using date(). Documentation for date: http://php.net/manual/en/function.date.php

To get the number of hours (4):

$timestamp = date("g",strtotime($yourdatetime));

To get the number of hours and minutes (4.03):

$timestamp = date("g.i",strtotime($yourdatetime));