【问题标题】:PHP, how to calculate a date range to get a valuePHP,如何计算日期范围以获取值
【发布时间】:2019-11-03 16:33:39
【问题描述】:

我在输入帖子时想计算日期范围时遇到问题。 但我的代码有错误:

Object of class DateInterval could not be converted to string

这是我的代码

 $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
 );

【问题讨论】:

    标签: php codeigniter date


    【解决方案1】:

    您从表单中获得了什么样的价值。我的意思是它是日期格式。这是一个使用 Datetime 并计算两个日期之间差异的示例。

    // 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'); 
    

    【讨论】:

    • 我想从输入中获取日期范围的值并进入数据库
    • 您获取的是 YYYY-MM-DD 格式的表单值吗?或者别的什么
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多