gpt4 book ai didi

php - 计算与之前 created_at 的时间差的最佳方法 - Laravel

转载 作者:行者123 更新时间:2023-12-05 06:48:08 26 4
gpt4 key购买 nike

我有一个婴儿日志列表,我尝试自动计算之前的时间差,减去喂食间隔。

feedingInterval = 3h

例如

如果我在下午 5 点喂我的宝宝,如果我在晚上 7:50 再次喂他。他早到了 10 分钟。

  • 超过3h,绿色(+)代表宝宝睡得好
  • 3h以内,红色(-)表示宝宝吃得早,不能太频繁。

enter image description here

我有这段代码

if($baby){

$dateTime = new \DateTime();
$date = $dateTime->format('Y-m-d');

if(Request::get('date') != null) {
$date = Request::get('date');
}

$yesterday = date('Y-m-d',strtotime("-1 days"));
$logFromYesterday = BabyLog::where('type', $type)->whereDate('created_at', $yesterday )->orderBy('created_at', 'desc')->where('babyId',$baby->id)->get()->first();


$babyLogs = BabyLog::where('type', $type)->whereDate('created_at', '=', $date)->orderBy('created_at', 'desc')->where('babyId',$baby->id)->get();


$logDetail = [];
$lastFeedStatus = 'n/a';
foreach ($babyLogs as $i => $feed) {

$id = $babyLogs[$i]->id;

$t1 = $babyLogs[$i]->updated_at;

if($i >= (count($babyLogs)-1)){

if($logFromYesterday){
$t2 = $logFromYesterday->updated_at;
} else {
$t2 = $date.' 00:00:00';
}

}else {
$t2 = $babyLogs[$i+1]->updated_at;
}

// dd($t1,$t2);//

$diffTime = abs(strtotime($t1) - strtotime($t2));
$diffTime = $diffTime / ( 60 * 60 );

if($type == 'feed'){
$diffTime = $diffTime - $baby->feedingInterval;

$diffTime = round($diffTime,2);
$diffTime = $diffTime*60;
$logDetailStatus = ($diffTime>0) ? "+" : "-";
$diffTime = $logDetailStatus.str_replace('-', '', $diffTime). "m ";

} else {

$diffTime = round($diffTime,2);
$diffTime = $diffTime*60;
$diffTime = str_replace('0', '',date('H\h', mktime(0,$diffTime))) . '';

}

$logDetail[$i]['id'] = $id;
$logDetail[$i]['msg'] = $diffTime;


if($i == 0){
$lastFeedStatus = $diffTime;
}

}

return $logDetail;

}

我只想四舍五入到分钟。

例。 10.2 米 --> 10 米


我试过了

round($diffTime,2);

round($diffTime,1);

我得到了

例。 10.2 米 --> 12 米

enter image description here

最佳答案

尝试使用 number_format():

<?php
echo number_format("100.1234");
echo number_format("100.50");
echo number_format("100.890");
?>

结果将是:

100

101

101

关于php - 计算与之前 created_at 的时间差的最佳方法 - Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66897418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com