-3

The timestamp in my database is 2015-03-03 00:25:39 (Take note that the type = timestamp and the correct current timestamp in my end is 2015-03-02 01:31:00. The difference should be around 23 hours. But now the problem is that the answers provided in the net will give me 30 hours instead of 23 hours. Some of the codes that I have tried are the following:

$target is the target date

CODE 1:
$then = strtotime($target);
$diff = $then - time();
echo sprintf("%s days and %s hours left", date('z', $diff), date('G', $diff));

But it gives me 1 days and 6 hours left. So 30 hours

CODE2:
$seconds = strtotime("$target") - time();
echo $seconds; exit();
$days = floor($seconds / 86400);
$seconds %= 86400;
$hours = floor($seconds / 3600);
echo $hours;

It gives me something like 107388 = 30 hours.

CODE 3:
//Convert to date
$datestr= $target;//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count) 
//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));

It gives me 6 hours

I don't know what I'm doing wrong, more like I have no idea how to do it. This is now my last resort since I can't find the solution that will help me. Hoping for your fast responses.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Dev
  • 1,592
  • 2
  • 22
  • 45
  • *"Hoping for your fast responses."* - Protip: Don't use that in questions. We're not on a deadline here. – Funk Forty Niner Mar 01 '15 at 17:41
  • It's currently 1:41am here and I would really like to have this to be done as soon as possible. Still many stuff to fix in our project. So yeah, I hope you understand. – Dev Mar 01 '15 at 17:42

2 Answers2

2

PHP's DateTime() (and DateInterval()) are much better for date math and returns the correct results:

$date = new DateTime('2015-03-03 00:25:39');
$now  = new DateTime('2015-03-02 01:31:00');
$diff = $date->diff($now);
echo $diff->h, ' hours ', $diff->i, ' minutes';

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

This is a very late answer, but your question is a good one that will likely be searched for in the future.

Here is an online demo.

// this doesn't appreciate any timezone declarations, you'll need to add this if necessary

$target="2015-03-03 00:25:39";  // declare your input
$then=new DateTime($target);    // feed input to DateTime
$now=new DateTime();         // get DateTime for Now
$diff=(array)$then->diff($now);      // calculate difference & cast as array
$labels=array("y"=>"year","m"=>"month","d"=>"day","h"=>"hour","i"=>"minute","s"=>"second");
$readable="";       // declare as empty string
// filter the $diff array to only include the desired elements and loop
foreach(array_intersect_key($diff,$labels) as $k=>$v){
    if($v>0){  // only add non-zero values to $readable
        $readable.=($readable!=""?", ":"")."$v {$labels[$k]}".($v>1?"s":"");
        //  use comma-space as glue | show value | show unit | pluralize when necessary
    }
}
echo "$readable";
// e.g. 2 years, 20 days, 1 hour, 10 minutes, 40 seconds
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @NotTestAcct I designed this solution for not only your case, but for anyone's case where they wish to output a human-readable time difference. If it satisfies, please award it a green tick so that your question is removed from the unanswered questions list. – mickmackusa Mar 23 '17 at 08:41