我一直在尝试修复这个php百分比计算器的一天…基本上现在是下午23点左右,我完成了一天工作的73% ....应该是每天60%的时间。下面是代码:
$now = time();
$today = strtotime(date("m/d/Y"));
$seconds = $now - $today;
$day = 24*60*60;
$percent = $seconds / $day*100;
我试着写我自己的版本,但我得到了100%的一天…下面是代码:
$todaysTime = time();
$todaysStart = time()-86400;
$todayCalc = $todaysTime - $todaysStart;
$dayPhpOne = 24*60*60;
$percentDay = $todayCalc / $dayPhpOne*100;
这是在php中完成的,我在哪里搞砸了我的代码?
试试这个:
$percentDay = time() % 86400 / 864;
编辑
从我得到的评论来看,我没有详细说明时区。让我澄清一下,这意味着UTC日百分比
此解决方案不考虑时区和其他与时间相关的复杂性:
$d = new DateTime();
$h = $d->format('H');
$m = $d->format('i');
$s = $d->format('s');
$currentSecond = $h * 3600 + $m * 60 + $s;
$midnight = new DateTime($d->format('Y-m-d'), $d->getTimezone());
$tomorrow = clone $midnight;
$tomorrow = $tomorrow->add(new DateInterval('P1D'));
$secondsToday = $tomorrow->getTimestamp() - $midnight->getTimestamp();
$percent = $currentSecond / $secondsToday * 100;
var_dump($percent);
如果有必要,可以指定一个特定的时区作为第二个DateTime
构造函数参数。