I have a list of baby logs, and I've tried to auto calculate the time difference between the previous ones, subtract the feeding interval.
feedingInterval = 3h
Ex.
If I fed my baby at 5PM, and if I fed him again at 7:50 PM. He's early 10 mins.
- More than 3h, it's green(+) means the baby sleep well
- Less than 3h, it's red(-) means the baby will feed early, and we shouldn't do too often.
I have this block of code
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;
}
I want to round to minutes only.
Ex. 10.2 m --> 10 m
I tried
round($diffTime,2);
to
round($diffTime,1);
I got
Ex. 10.2 m --> 12 m