简体   繁体   中英

How to count long days from one date to another date and insert to table of database using CI

My Table in MySQL

I designed "survei_pohon" table and I set my 'submit_date' column has same values (default values, ex: 2013-02-28), and this is my table below:

id          survey_date           submit_date         long_day
 1           2013-02-18           2013-02-28             10
 2           2013-02-21           2013-02-28             10
 3           2013-02-25           2013-02-28             10   

"id" column is 'integer' type, "survey_date" is 'date' type, "submit_date" is 'date' type, "long_day" is 'integer' type.

This my Model : `

function olah(){
$tanggal_survei="SELECT survey_date FROM survei_pohon";
$tanggal_sekarang="SELECT submit_date FROM survei_pohon";
$lama_hari="SELECT long_day FROM survei_pohon";


    $result = @mysql_query($tanggal_survei);
    $t = mysql_fetch_array($result);

    $results = @mysql_query($tanggal_sekarang);
    $u = mysql_fetch_array($results);

    $lama_hari = @mysql_fetch_array($lama_hari);

    $start = strtotime($t['tanggal_survei']);
    $end = strtotime($u['tanggal_sekarang']);

    $lama_hari = ($end - $start) / (60 * 60 * 24);

    $this->db->set('long_day', $lama_hari);
    $this->db->update('survei_pohon');        
}

}?>`

My expectation from my "survei_pohon" table is

id          survey_date           submit_date        long_day
 1           2013-02-18           2013-02-28             10
 2           2013-02-21           2013-02-28              7
 3           2013-02-25           2013-02-28              3

I'm guessing that there is something wrong in my model code. But I don't know how to fix that code. I'm expecting your help. Thanks for any help, this is difficult for me.

Here's a MySQL solution to update the db.

UPDATE survei_pohon SET long_day = submit_date - survey_date

Not sure you really need to store that field though since it's a calculation of other fields -- generally leave those for UI purposes. In which case, you could just include it in your SELECT statement:

SELECT *, submit_date - survey_date long_day FROM survei_pohon

I know this doesn't address how to do it with your code, but seems a little easier.

Here is the SQL Fiddle .

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