3

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.

enter image description here

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

enter image description here

code-8
  • 54,650
  • 106
  • 352
  • 604
  • 2
    Is there any reason why you are not using Carbon? Carbon makes these types of calculations a lot easier. – Praveesh Apr 01 '21 at 02:19
  • @Praveesh If it's doable via PHP, I would prefer that, will Carbon won't add much weight to page load ? – code-8 Apr 01 '21 at 02:23
  • 4
    Carbon is loaded when Laravel loads(`Illuminate\Support\Carbon`) and Laravel internally uses it for the date manipulations. So it wouldn't have much effect on the performance if you use it here as well. – Praveesh Apr 01 '21 at 02:28
  • 1
    If you really want to use php then dateTime() or strtotime() is your option for doing that. – Farhan Ibn Wahid Apr 01 '21 at 03:26
  • see this answer https://stackoverflow.com/a/3923228/8607640 – Farhan Ibn Wahid Apr 01 '21 at 03:30
  • I personally always using Carbon for date manipulation in Laravel. Try using Carbon, it shouldn't affect much in terms of performance. – klaudiusnaban Feb 23 '22 at 13:54

1 Answers1

0

Try using number_format():

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

Result will be:

100

101

101

Abbasi
  • 31
  • 6