简体   繁体   中英

PHP, how to calculate a date range to get a value

I have a problem when, input post, want to calculate the date range. but my code has an error:

Object of class DateInterval could not be converted to string

this my code

 $id_barang       = $this->input->post('id_barang');
 $id_rak          = $this->input->post('id_rak');
 $id_tf           = $this->input->post('id_tf');
 $id_user         = $this->input->post('id_user');
 $qty_outbound    = $this->input->post('qty_outbound');
 $tgl_outbound    = $this->input->post('tgl_outbound'); // date out
 $tgl_tf          = $this->input->post('tgl_tf'); // date in

 $tanggal  = new DateTime($tgl_tf);
 $today    = new DateTime($tgl_outbound); 
 $selisihnya = $today->diff($tanggal);
 $selisihnya->d = $this->input->post('selisih');     



 $data_insert = array(
'jenis'            => 'outbound',
'id_barang'        => $id_barang,
'id_rak'           => $id_rak,
'id_tf'            => $id_tf,
'id_user'          => $id_user,
'qty_outbound'     => $qty_outbound,
'tgl_outbound'     => $tgl_outbound,
'tgl_tf'          => $tgl_tf,
'selisih'        =>$selisihnya
 );

What kind of value you are getting from the form. I mean is it in the date format. Here is an example to use Datetime and calculate the difference between two dates.

// Initialising the two datetime objects 
$datetime1 = new DateTime('2019-9-10'); 
$datetime2 = new DateTime('2019-9-15'); 

// Calling the diff() function on above 
// two DateTime objects 
$difference = $datetime1->diff($datetime2); 

// Getting the difference between two 
// given DateTime objects 
echo $difference->format('%R%a days'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM