2

这似乎应该很简单,但我找不到我需要的时间格式。

来自数据库的值为 240。这意味着 240 分钟。如何将其存储在 php 变量中,以便 php 知道它的分钟数。所以稍后在脚本中我可以将它添加到一个HH:MM值?

(我编辑了下面的代码以反映其中一个答案)

$startTime = new datetime($row['startTime']);  #08:07:00.0000000
$endTime = new datetime($row['endTime']);      #12:10:00.0000000
$everyMinutes = new dateInterval('P'.$row['everyMinutes'].'M'); #60?
$updatedTime = $startTime->add($everyMinutes); # this should read 09:07:00.0000000

进入的日期$row来自sql。startTime 和 endTime 是time(7)数据类型

| taskID |    startTime     |     endTime      | freq |
|________|__________________|__________________|______|
|   1    | 08:07:00.0000000 | 12:10:00.0000000 |  60  |
|   2    | 08:10:00.0000000 | 17:40:00.0000000 |  30  |
|   3    | 08:40:00.0000000 | 14:49:00.0000000 |  60  |
|   4    | 08:43:00.0000000 | 14:49:00.0000000 |  60  |
|   5    | 09:05:00.0000000 | 15:05:00.0000000 |  180 |
|   6    | 10:00:00.0000000 | 22:00:00.0000000 |  5   |

使用此代码,我遇到了两个问题之一。抛出new datetime()的错误是Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, object given'

没有new datetime()抛出的错误是Notice: Object of class DateTime could not be converted to int

4

2 回答 2

4

你应该阅读这个页面

$date = new DateTime($row['startTime']);
$myInterval = new DateInterval('P'.$row['everyMinutes'].'M');
$date->add($myInterval);
echo $date->format('Y-m-d') . "\n";
于 2013-04-16T11:17:10.073 回答
0

使用此功能

function convertToHoursMins($time, $format = '%d:%d') {
settype($time, 'integer');
if ($time < 1) {
    return;
}
$hours = floor($time/60);
$minutes = $time%60;
return sprintf($format, $hours, $minutes);
}

像这样打发时间

convertToHoursMins(240);
于 2013-04-16T11:20:01.323 回答