⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Calculate date



gersontk
  • profile picture
  • Member

Posted 20 February 2012 - 20:57 PM

I have two date fields. start date and end date. How 10.1.2012 and 20.1.2012. I want to calculate the difference between them in days, what should I do? someone could help me?

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 20 February 2012 - 21:34 PM

This is easy:


function duration_calc($start, $end){//The format must be Y-m-d , m-d-Y, Y/m/d
$days = (strtotime($end) - strtotime($start)) / (60 * 60 * 24);
return $days;
}


I also have a helper that I've created ( it is old but it works ) that extend the date_helper of codeigniter: [attachment=52:date_helper.php]

It is better thought to ask this kind of questions at the Codeigniter forum at: http://codeigniter.com/forums/ because perhaps your questions will not be answered here if there is not for the library of grocery CRUD.

Kindest Regards

Johnny

gersontk
  • profile picture
  • Member

Posted 20 February 2012 - 23:38 PM

Thank you. sorry, I asked here because I'm using the library. I have two fields that I mentioned and was not able toimplement, but will solve the problem here, thanks for the idea.

gersontk
  • profile picture
  • Member

Posted 21 February 2012 - 02:52 AM

I need a little help, here I am changing the code. calculadia is the function that counts the days, but when I insert the callback in the column for each record should show his days he only shows one record ... same as I do so that each record has its days calculated?


public function layout(){
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('template');
$this->db->order_by('cliente','asc');
$crud->set_relation('cliente','cliente','nome');
$crud->set_relation('responsavel','responsavel','nome');
$crud->set_relation('situacao','tipo','nome');
$crud->callback_column('tempo',array($this,'calculodia'));
$crud->set_subject('para Responsavel');
$output = $crud->render();
$this->_example_output($output);
}
public function calculodia(){
$res = $this->db->get('template');
foreach ($res->result() as $linha){
return (strtotime($linha->data_final) - (strtotime($linha->data_inicio))) / (60 * 60 * 24). " dias"."<br/>";
}
}

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 21 February 2012 - 07:29 AM

First of all you forgot to use the method columns. So for example you will have:


...
$crud->columns('cliente','responsavel','situacao','tempo');
$crud->callback_column('tempo',array($this,'calculodia'));
...



And the callback that you use is wrong. In every callback I have some parameters that I pass through the callback. The $value and $row are auto-passed so you will not need anything else to add to your code. I know the documentation is not so good to understand that , so you will just need:

public function calculodia($value, $row){
return (strtotime($row->data_final) - (strtotime($row->data_inicio))) / (60 * 60 * 24). " dias";
}

gersontk
  • profile picture
  • Member

Posted 21 February 2012 - 14:16 PM

Now I understand ... it worked. Thanks, I'll study more ...

likhon3k
  • profile picture
  • Member

Posted 18 May 2013 - 08:00 AM

how to pass multiple date into call back function? like "start date", "end date" into call back function.


likhon3k
  • profile picture
  • Member

Posted 20 May 2013 - 08:30 AM

unable to solve this problem. I want to calculate date between two days. i want to make leave managment page. please see the code.

 

function leave_management()
    {
          
            $crud = new grocery_CRUD();

            $crud->set_table('leave_management');

            $crud->columns('employee_name','from_date','to_date','duration');
            $crud->callback_field('duration',array($this,'callback_to_date'));
            $output = $crud->render();
            
            $this->_example_output($output);
    }
    
    
    function callback_to_date($from_date, $to_date)
    {//The format must be Y-m-d , m-d-Y, Y/m/d
   
        $days = (strtotime($from_date) - strtotime($to_date)) / (60 * 60 * 24);
        return $days;
    }

 


davidoster
  • profile picture
  • Member

Posted 20 May 2013 - 11:30 AM

The callback_field is used when you need to make changes to a specific field.

What you are trying to do is not achieved by using callback_field but by using callback_column where on a separate column you calculate the difference.


likhon3k
  • profile picture
  • Member

Posted 21 May 2013 - 05:42 AM

The callback_field is used when you need to make changes to a specific field.

What you are trying to do is not achieved by using callback_field but by using callback_column where on a separate column you calculate the difference.

 

Thnaks for your reply. I used both callback founcion but failed to solve it.

 

can you please tell me the solution ?


likhon3k
  • profile picture
  • Member

Posted 21 May 2013 - 07:54 AM

Problem solved by changing the date format in grocercrud config file.

 

can any one tell me, how could I change date format from UK date to SQL date format without chaning orginal grocery crud file?

 

suppose, I have a date like dd-mm-yyyy format. I want to convert it to yyyy-mm-dd format.


davidoster
  • profile picture
  • Member

Posted 21 May 2013 - 08:17 AM

Changing the date in the config file is the preferred way!

It is not a change on the functionality of GC so you can change the settings there depending on your system.


likhon3k
  • profile picture
  • Member

Posted 24 May 2013 - 16:08 PM

Changing the date in the config file is the preferred way!

It is not a change on the functionality of GC so you can change the settings there depending on your system.

 

thanks. can you tell me how can I save the calculated data into database? I found the callback_column function result is not inserted into database. but i want to save the date calculation result. can you please give me idea of this problem to solve?


davidoster
  • profile picture
  • Member

Posted 25 May 2013 - 05:05 AM

If you need to save the result you must also use the callback_field.


Manoj Fernando
  • profile picture
  • Member

Posted 27 April 2016 - 08:46 AM

If you need to save the result you must also use the callback_field.

Hi,

 

Please post the callback_field code to save the difference of dates to database