计算一天中事件发生的时间


calculating what second of the day an event occurred

我编写了一些有效的代码,但效率似乎很低。我在画布上画圆圈,以显示工作日中发生的事件。为了做到这一点,我找到了发生这种情况的一天的秒数,然后将其转换为分钟来显示。

我的问题是,为什么我必须转到strtotime(date('...',strtotime(time)))并调用strtotime两次?

当我把它当作strtotime(date('Y-m-d',time)时,我会得到一个负值。

<?php
$time_concerned = '2014-10-31 02:00:00';
$start_day_temp = strtotime(date('Y-m-d',strtotime($time_concerned)));
$end_day_temp = strtotime(date('Y-m-d H:i',strtotime($time_concerned)));
$num_seconds = $end_day_temp - $start_day_temp;
//divide by 60 to get minutes.  divide in half to make fit on 720 scale.
$num_conv_minutes = (($num_seconds/60)/2);
echo '  ctx.fillStyle = "#00FF00";';
echo '  ctx.beginPath();';
echo '  ctx.arc('.$num_conv_minutes.'+ x_inbound,y_truck_arrivals,5,0,2*Math.PI);';
echo '  ctx.fill();';
echo '  ctx.stroke();';
?>
$time_concerned = '2014-10-31 02:00:00';
echo strtotime(date('Y-m-d',strtotime($time_concerned))); 
echo "'r'n"; 
echo strtotime($time_concerned);

试试这个。不停摆弄

这些几乎是同一件事。它们不同的唯一原因是,当你把字符串转换成日期时,你会去掉第二分钟和第二小时,所以PHP把它放在午夜,你会失去告诉你在凌晨2点做了这一切的部分。

这在您第一次使用它时很有用,因为您需要从另一个时间戳中减去午夜的时间戳。然而,第二次它只是多余的。

下面是你的操作方法。

$time_concerned = '2014-10-31 02:00:00';
$start_day_temp = strtotime(date('Y-m-d',strtotime($time_concerned)));
$end_day_temp = strtotime($time_concerned);