Date Function & Time Difference in PHP

Asked By 400 points N/A Posted on -
qa-featured

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
SHARE
Best Answer by Karen_Speak
Best Answer
Best Answer
Answered By 0 points N/A #113756

Date Function & Time Difference in PHP

qa-featured

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
?>

Answered By 0 points N/A #113757

Date Function & Time Difference in PHP

qa-featured

 

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:

Related Questions