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
- Login or Signup Now to post comments

Hi Patrick,
<?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
?>