Time Difference problem in PHP and MySQL and its solution

Using  the while loop you can get these values one by one  :

//php part

/**************************************************/

while($rst_results = mysql_fetch_array($results))

{

     //showinf the difference string by passing 4 values

     echo dateTimeDiff($rst_results[‘dataDif’],$rst_results[‘years ‘],$rst_results[‘months’],$rst_results[‘days’]);

}

/**************************************************/

You can see the php part we are calling the function dateTimeDiff with 4 parameters that we are getting from the Query : i will write the function below before that i want to tell you the format and about the 4 cluses

1 – TIMEDIFF(NOW(),`CreatedDate`) as dataDif

TIMEDIFF is a mysql function we are passing NOW() [ a mysql function generate present time in yyyy-mm-dd hr:min:sec format ] and CreatesDate which is the fields value – it will generate a new columns as dateDif inhours:min:seconds format hours can be 234 hours between 2 days

2 – TIMESTAMPDIFF(YEAR,`CreatedDate`,NOW()) as years

TIMESTAMPDIFF is also a mysql function in which we can generate diference date , month or years by passing first parameter

here you can see the first parameter is YEAR so it will just return the number of YEARS difference.

3 – (TIMESTAMPDIFF(MONTH,`CreatedDate`,NOW()) – (TIMESTAMPDIFF(YEAR,`CreatedDate`,NOW()) * 12)) as months

After years i need to get the remaining months in difference so here i have generate the MONTHS as TIMESTAMPDIFF(MONTH,`CreatedDate`,NOW())

which will generate the months difference between 2 dates and after that subtracting the number of months of years i got

means suppose you got 2 years so if –TIMESTAMPDIFF(MONTH,`CreatedDate`,NOW())  is giving you 28 months of 2 dats difference then you need to show only 4 months as for 2 years 12*2 = 24 months subtracted so the string will be : 2 years 4 months

4 – (TIMESTAMPDIFF(DAY,`CreatedDate`,NOW()) – (TIMESTAMPDIFF(MONTH,`CreatedDate`,NOW()) * 30)) as days

In the same way we need to generate the left days so : number of days – (months*30) . (TIMESTAMPDIFF(DAY,`CreatedDate`,NOW())  will return the number of days and TIMESTAMPDIFF(MONTH,`CreatedDate`,NOW())will return the number of months , So in this way we can get the left days

dateTimeDiff Function in PHP 

dateTimeDiff Function in PHPfunction dateTimeDiff($datadiff,$years,$months,$days)
 { 
  $string = '';
  if($years != 0)
  {
   $string = $string.$years." years ";
  }
  if($months != 0)
  {
   $string = $string.$months." months ";
  }
  if($days != 0)
  {
   $string = $string.$days." days ";
  }
  if($days == 0)
  {
   $datadiff = explode(":",$datadiff );
   $hr  = $datadiff[0];
   $min = $datadiff[1]; 
   $sec = $datadiff[2];
   if($hr != 0)
   {$string = $string.",".$hr." hours "; }
   if($min != 0)
   {$string = $string.$min." minuts "; }
   if($min == 0)
   {$string = $string.$sec." Seconds "; }
  }
  $string = $string." ago ";
  return $string;
 }
 
/* According to your requirement for date difference strin you can change this function to get your formated string*/
150 150 Burnignorance | Where Minds Meet And Sparks Fly!