⚠ 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

how to compute bday and displat Age



DREON

DREON
  • profile picture
  • Member

Posted 20 April 2013 - 02:25 AM

good day to all master of CI and GC, i have a problem on  how can the Birthday and display age on the other column

 

example i have an info table with column

 

id | lname | fname | birth_day(yyyy-mm-dd) | age

1   robert     don       1987-8-6                           my problem is here i wnat to show the age.. thnx


davidoster

davidoster
  • profile picture
  • Member

Posted 21 April 2013 - 00:31 AM

http://www.grocerycrud.com/documentation/options_functions/callback_field


heruprambadi

heruprambadi
  • profile picture
  • Member

Posted 21 April 2013 - 05:49 AM

use callback function :

 

$crud->callback_field('birthday',array($this,'callback_age'));

 

 

and use this function for callback :

 

function callback_age($value, $row)
{
	$diff = abs(strtotime(date('Y-m-d')) - strtotime($value));
	$years = floor($diff / (365*60*60*24));
return $years." years Old";
}

DREON

DREON
  • profile picture
  • Member

Posted 21 April 2013 - 06:14 AM

thank you masters..


DREON

DREON
  • profile picture
  • Member

Posted 21 April 2013 - 06:37 AM

i use this but it only appear on edit view, how can i view the on the GC table, i mean on flexigrid or datatable..

 

also appear on my bday column not on age column..

 

i want like this

 

id | lname | fname | birth_day(yyyy-mm-dd) | age


1   robert     don       1987-8-6                      

 

$crud->callback_field('birthday',array($this,'callback_age'));

 

function callback_age($value, $row)
{
    $diff = abs(strtotime(date('Y-m-d')) - strtotime($value));
    $years = floor($diff / (365*60*60*24));
return $years." years Old";
}


davidoster

davidoster
  • profile picture
  • Member

Posted 21 April 2013 - 06:55 AM

1. use the columns function and add to the end 'age'

2. use callback_column calculate and display the age on the grid (you can use at the same time callback_field as above)

3. use the callback_age within callback_column


DREON

DREON
  • profile picture
  • Member

Posted 21 April 2013 - 07:31 AM

i cant understand the

 

3. use the callback_age within callback_column?

 

this is my code now

$crud->columns('name','bday','age');

 

$crud->callback_column('age',array($this,'callback_age'));

 

 

    function callback_age($value, $row)
{
                    $diff = abs(strtotime(date('Y-m-d')) - strtotime($value));
                    $years = floor($diff / (365*60*60*24));
                    return $years." years Old";
}

 

when i try this $crud->callback_column('bday',array($this,'callback_age')); i dont have problem its correct

 

but i want is to view the compputed bday to age column

 

my only problem is to view the age on the age column...


DREON

DREON
  • profile picture
  • Member

Posted 21 April 2013 - 07:52 AM

sir thank you i solve the problem.. i read your step by step again

 

i change

the

 $diff = abs(strtotime(date('Y-m-d')) - strtotime($value));

to

 

$diff = abs(strtotime(date('Y-m-d')) - strtotime($row->bday));


DREON

DREON
  • profile picture
  • Member

Posted 21 April 2013 - 08:04 AM

i have a little problem how can i display the the months?

 

example on my code above the output result is

 

25 years old

 

i want is

 

25 years old and 4 months


heruprambadi

heruprambadi
  • profile picture
  • Member

Posted 21 April 2013 - 08:51 AM

i have a little problem how can i display the the months?

 

example on my code above the output result is

 

25 years old

 

i want is

 

25 years old and 4 months

 

this is exactly what are you looking for :

public function callback_age($value, $row)
{
	$diff = abs(strtotime(date('Y-m-d')) - strtotime($value));
	$years = floor($diff / (365*60*60*24));
	$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  return $years." Years".$months." Month";

heruprambadi

heruprambadi
  • profile picture
  • Member

Posted 21 April 2013 - 08:53 AM

sir thank you i solve the problem.. i read your step by step again

 

i change

the

 $diff = abs(strtotime(date('Y-m-d')) - strtotime($value));

to

 

$diff = abs(strtotime(date('Y-m-d')) - strtotime($row->bday));

wow.. really ? i don't need to change it like this, it works for me.

but more importantly, your problem is solved B)


DREON

DREON
  • profile picture
  • Member

Posted 21 April 2013 - 11:40 AM

wow.. really ? i don't need to change it like this, it works for me.

but more importantly, your problem is solved B)

 

thank you sir, your code help me a lot, i changed to $diff = abs(strtotime(date('Y-m-d')) - strtotime($row->bday));

to view on grid age column not on bday column..

 

this is the example output/result:

 

name |  bday             | age(my callbacl column)

robert   1987-8-6               25 years old      


heruprambadi

heruprambadi
  • profile picture
  • Member

Posted 21 April 2013 - 11:55 AM

you're welcome, dreon. how about the months ?

is it works ?


DREON

DREON
  • profile picture
  • Member

Posted 21 April 2013 - 13:56 PM

@heruprambadi, yes sir, thank you.. 100% working.. how about days ?^^ for future purposes..


heruprambadi

heruprambadi
  • profile picture
  • Member

Posted 21 April 2013 - 14:35 PM

$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

 

but, since not all years have 365 day (sometime 364 years), this is not exactly right. just miss calculate a few days :p

is anyone have solution ?


davidoster

davidoster
  • profile picture
  • Member

Posted 21 April 2013 - 21:58 PM

http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php


heruprambadi

heruprambadi
  • profile picture
  • Member

Posted 22 April 2013 - 01:52 AM

it's the same think. it also miss calculate a few days.


DREON

DREON
  • profile picture
  • Member

Posted 22 April 2013 - 03:49 AM

many thanx for help masters.


Dale

Dale
  • profile picture
  • Member

Posted 22 April 2013 - 07:12 AM

This seems like the best way to calculate the time/date differences - using PHP's interval calculations...
 
 

 

    function callback_col($value, $row)
    {    
        $date1 = new DateTime("now");
        $date2 = new DateTime($row->dob);
        $interval = $date1->diff($date2);
        error_log( "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ");
        return $interval->y;
    }

 


davidoster

davidoster
  • profile picture
  • Member

Posted 22 April 2013 - 14:34 PM

http://stackoverflow.com/questions/1383516/date-difference-in-mysql-to-calculate-age