Hello,
I'm still new in PHP and there's 2 case actually that I want to ask.
Â
For case 1, there's a string variable:
Â
$dateSearch = "Thu Jun 9 5:42:58 +0000 2011";
Â
What I want to know is how can I get the result of time difference between now (today and this hour) and $dateSearch. The output format will be like 5 Days 7 Hours 25 Minutes 10 Seconds. Can this be done? I'd really appreciate your help.
Â
For case 2, let's say the current date and hour in my laptop is June 9th 2011 11:55.
Â
When I use the function date like this:
Â
echo date("d M Y H:i:s");
Â
The result would be June 9th 2011 04:55. There's a 7 hour difference. My time zone is GMT +7 so I was thinking this probably having to do with the result but I'm not really sure about it. Any ideas on how to confirm about it? Are there any steps to debug this kind of problem?
Â
Btw I'm using Windows XP SP3 with 2 GB RAM and 250 GB HDD. I don't know if it's important or not but just in case you need more information about the laptop that I'm using.
Â
Well that's 2 case from me. Looking forward for your suggestions and ideas guys.
Â
Regards,
Â
Patrick
Date Function & Time Difference in PHP
Hi Patrick,
-
I had a similar requirement maybe two weeks ago when I had to write a utility module for an online store.
-
Here is a sample code which should help you.
<?PHP
   $date1 = new DateTime('2011-06-10');
   $date2 = new DateTime("now");
   $interval = $date1->diff($date2);
   $years = $interval->format('%y');
   $months = $interval->format('%m');
   $days = $interval->format('%d');
   if($years!=0){
       $ago = $years.' year(s) ago';
   }else{
       $ago = ($months == 0? $days.' day(s) ago': $months.' month(s) ago');
   }
   echo $ago;
?>
 If used on the same day, It will return all 0's in the format.
 <?PHP
       $date1 = new DateTime('2011-05-161');
   $date2 = new DateTime("now");
   $interval = $date1->diff($date2);
   $diff = $interval->format('%y-%m-%d');
   echo $diff; //Today, this will output 0-0-0
?>
Date Function & Time Difference in PHP
Â
The main concept is that convert your current and final date string into time stamp to get accuracy.
Â
$start = strtotime('Thu Jun 9 5:42:58 +0200 2011'); //Start Date
Â
$end = time();//Current Date
Â
if($t1 > $t2) //Compare
Â
  {
Â
   $time1 = $t2;
Â
   $time2 = $t1;
Â
  }
Â
  else
Â
  {
Â
   $time1 = $t1;
Â
   $time2 = $t2;
Â
  }
Â
  $diff = array(
Â
   'years' => 0,
Â
   'months' => 0,
Â
   'weeks' => 0,
Â
   'days' => 0,
Â
   'hours' => 0,
Â
   'minutes' => 0,
Â
   'seconds' =>0
Â
  );
Â
//Make an array for date strings ..
Â
  foreach(array('years','months','weeks','days','hours','minutes','seconds')
Â
     as $unit)
Â
  {
Â
   while(TRUE)
Â
   {
Â
     $next = strtotime("+1 $unit", $time1);
Â
     if($next < $time2)
Â
     {
Â
      $time1 = $next;
Â
      $diff[$unit]++;
Â
     }
Â
     else
Â
     {
Â
      break;
Â
     }
Â
   }
Â
  }
Â
output = "The difference is:";
Â
foreach($diff as $unit => $value)
Â
{
Â
  echo " $value $unit,";
Â
}
Â
$output = trim($output, ',');
Â
echo $output;//
Â
/// 0 years, 1 months, 2 weeks, 1 days, 11 hours, 47 minutes, 14 seconds,The difference is: