我需要两个专栏,durasi和mulai,我希望总列反映durasi - mulai。
这是我的控制者。
public function postSimpanTambah()
{
$simpan= array();
$simpan['nama']=Request::input('nama');
$simpan['tanggal']=Request::input('tanggal');
$simpan['deskripsi']=Request::input('deskripsi');
$simpan['mulai']=Request::input('mulai');
$simpan['durasi']=Request::input('durasi');
$simpan['total'] = \Carbon\Carbon::createFromFormat('H:i', Request::input('durasi')) - \Carbon\Carbon::createFromFormat('H:i', Request::input('mulai'));
DB::table('lembur_karyawan')->insert($simpan);
echo "<script language='javascript'>
alert('Berhasil menambahkan data');
document.location='../user';
</script>";
}
这是我的观点:
<h3>Tambah User</h3>
<hr>
<form method="post" action="<?php echo url('user/simpan-tambah') ?>" enctype="multipart/form-data">
<div class="form-group">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<input type="hidden" class="form-control" name="id" placeholder="Tambahkan Id">
</div>
<div class="form-group">
<label for="inputIsiBerita"> Nama:</label>
<input type="text" class="form-control" name="nama" value="<?php echo Auth::user()->name. '';?>" readonly>
</div>
<div class="form-group">
<label for="inputIsiBerita"> Tanggal:</label>
<input type="date" class="form-control" name="tanggal" placeholder="Tambahkan Tanggal">
</div>
<div class="form-group">
<label for="inputIsiBerita"> Deskripsi:</label>
<input type="text" class="form-control" name="deskripsi" placeholder="Tambahkan Deskripsi">
</div>
<div class="form-group">
<label for="inputIsiBerita"> Mulai:</label>
<input type="time" class="form-control" name="mulai" value="17:00" readonly>
</div>
<div class="form-group">
<label for="inputIsiBerita"> Durasi:</label>
<input type="time" class="form-control" name="durasi" placeholder="Tambahkan Durasi">
</div>
<div class="form-group">
<div class="controls">
<button type="submit" class="btn btn-primary">Kirim</button>
<button type="reset" class="btn btn-inverse">Reset</button>
<button type="button" onclick='location.href="<?php echo url('admin') ?>"' class="btn btn-inverse">Batal</button>
</div>
</div>
</form>
当我尝试跑步时,我总是得到回应: Carbon \ Carbon类的对象无法转换为int
我该怎么办?有想法吗?
答案 0 :(得分:0)
请尝试下面的时差代码。
$a = new DateTime(Request::input('durasi'));
$b = new DateTime(Request::input('mulai'));
$interval = $a->diff($b);
$x= $interval->format("%H:%i");
dd($x);
答案 1 :(得分:0)
使用Carbon提供的方法(Link to methods)来区分两个日期对象而不是逻辑运算符。
答案 2 :(得分:0)
检查这个。它会帮助你。我将时间转换为秒,然后减去秒,然后再将结果转换为小时和分钟。 durasi = 01:59,mulai = 17:00
$a = strtotime(Request::input('mulai'));
$b = strtotime(Request::input('durasi'));
$diff=$b-$a;
$time= gmdate("H:i", $diff);
dd($time);
示例:
$a=strtotime('17:00');;
$b=strtotime('01:59');
$diff=$b-$a;
$x= gmdate("H:i", $diff);
OUTPUT: "08:59"
答案 3 :(得分:0)
这是我的控制器。总和
public function getMei()
{
$data['users'] = DB::table('lembur_karyawan')
->select('nama', DB::raw('SUM(total) as total_lembur'))
->groupBy('nama')
->havingRaw('SUM(total)')
->first();
return view('mei_user', $data);
}
和我的观点:
<?php echo $users->{'total_lembur'} ?>
答案 4 :(得分:0)
添加两个日期
$a = strtotime("01:45")-strtotime("00:00");
$b = strtotime("02:25")-strtotime("00:00");
$seconds=$a+$b;
$time= gmdate("H:i:s", $seconds);
dd($time);