将int 60转换为60分钟的时间值 [英] Convert int 60 to a time value of 60 minutes
问题描述
这似乎很简单,但是我找不到我需要的时间格式。
This seems it should be trivially simple but I can't find the time format I would need.
数据库中的值为240。这表示240分钟。如何将其存储在php变量中,以便php知道它的分钟数。这样,在脚本的后面,我可以将其添加到 HH:MM
值吗?
A value comes from the database as 240. This means 240 minutes. How can I store this in a php variable so that php knows it's minutes. So that later in the script I can add it to a HH:MM
value?
(我已经编辑了下面的代码以反映答案之一)
(I have edited the code below to reflect one of the answers)
$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)
The date coming into $row
comes from sql. startTime and endTime are of time(7)
datatype
| 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()
时,引发的错误是致命错误:消息为'DateTime :: __ construct()的未捕获异常'Exception'期望参数1为字符串,给定对象'
With this code I am getting one of two issues. With new datetime()
the error thrown is Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects parameter 1 to be string, object given'
没有 new datetime()
时抛出的错误是注意:DateTime类的对象无法转换为int
推荐答案
您应该阅读此页面。
$date = new DateTime($row['startTime']);
$myInterval = new DateInterval('P'.$row['everyMinutes'].'M');
$date->add($myInterval);
echo $date->format('Y-m-d') . "\n";
这篇关于将int 60转换为60分钟的时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!