1

So, I had to find an interval between two dates, one is from system time, and one is from the database. First problem is, I used get() so it returns an array from the database and I have absolutely no idea on how to work on it. Can anyone tell me what is the best way to deal with this? I just need to get the number of days between $tenggat and $curdate.

public function tower_kecamatan(Request $r){
        $kode_kecamatan = $r->kode_kecamatan;
        $region = DB::table('region')
                    ->where('kode_kecamatan',$kode_kecamatan)->first();
        $data = DB::table('tower')
        ->leftJoin("perusahaan","perusahaan.tower_id","=","tower.tower_id")
        ->select('tower.*','perusahaan.*')
                ->where('kode_kecamatan', $kode_kecamatan)->get();
        $tenggat = DB::table('tower')
        ->select('tower.tenggat_izin')->get();
        $curdate = Carbon::now();        
    
        return view('tower_kecamatan', compact('region','data','tenggat'));
    }
Manoj
  • 2,059
  • 3
  • 12
  • 24
  • the "best" way is to use eloquent models and add the date attribute to the "Date mutators" (read more here: https://laravel.com/docs/8.x/eloquent-mutators#date-mutators )...then you get to operate on carbon instances directly – Rok Sprogar Nov 05 '20 at 13:22
  • you may want to check [Carbon](https://stackoverflow.com/questions/39508963/calculate-difference-between-two-dates-using-carbon-and-blade) – Santa's helper Nov 05 '20 at 13:24

1 Answers1

0
$tenggat = DB::table('tower')->select('tower.tenggat_izin')->get();

return the collection of objects, and you need just first one.

$rows = DB::table('tower')->select('tower.tenggat_izin')->get();
$tenggat = $rows[0]; 
$diff = Carbon::now()->diffInDays(Carbon::parse($tenggat->tenggat_izin));
$diff2 = Carbon::parse($tenggat->tenggat_izin)->diffInDays(Carbon::now();

I am not sure about field naming, but this code returns the diff in days between the current date and tenggat_izin field from table tower

It's not a good way, as said above use eloquent, and it will be much easier

KyleK
  • 4,643
  • 17
  • 33
V-K
  • 1,297
  • 10
  • 28