2

I have two values:

$c_time = 2016-03-21 14:56:05 
$e_time = 2016-03-24 14:56:05 

Now I want to show remaining time and days like this:

3 days 0 hour 0 minute 0 second

How I can do this using PHP?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Joe
  • 51
  • 2
  • 11
  • 6
    Have a look at PHP's [DateTime](http://php.net/DateTime) class. In particular, the [diff](http://php.net/manual/en/datetime.diff.php) method. – Jonnix Mar 22 '16 at 09:37
  • Modify the question, don't add code to comments. – Jonnix Mar 22 '16 at 09:40

4 Answers4

2

You can try this

$c_time = new DateTime('2016-03-21 14:56:05'); 

$e_time = new DateTime('2016-03-24 14:56:05');  

$date_diff = $c_time->diff($e_time);

echo "{$date_diff->days} days {$date_diff->h} hour {$date_diff->i} minute {$date_diff->s} second";
Uttam Kumar Roy
  • 2,060
  • 4
  • 23
  • 29
0

- first way:

$datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
$datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
$secs = $datetime2 - $datetime1;// == return sec in difference
$days = $secs / 86400;

- Another way:

$date1=  new DateTime("May 3, 2012 10:38:22 GMT");
$date2= new DateTime("06 Apr 2012 07:22:21 GMT");
echo $date1->diff($date2)->("%d");
  • It would be useful to use the dates in the question or at least the date format used in the question. – RiggsFolly Mar 22 '16 at 09:47
  • If OP has problems differentiating between $datetime1 & $datetime2 and understanding they reflect his own 2 dates as an example, I don't think alot of explaining would help here. Then again, I truly believe he would understand that. – Erez.Matrix Mar 22 '16 at 09:51
  • I am only trying to help you produce better answers in the future. Look at the other answer, not much difference to yours and yet he gets 2 upvotes. – RiggsFolly Mar 22 '16 at 10:08
0
$c_time = new DateTime('2016-03-21 14:56:05');
$e_time = new DateTime('2016-03-24 14:56:05');
$interval = $c_time->diff($e_time);
print $interval->format('%a days %h hour %i minutes %s seconds');

print $interval->days; // to obtain the number of days
print $interval->h; // to obtain the number of hours
print $interval->i; // to obtain the number of minutes
print $interval->s; // to obtain the number of seconds
0

You can use date_diff():

$c_time = "2016-03-21 15:56:05";
$e_time = "2016-03-24 15:56:05";
$start = date_create($c_time);
$end = date_create($e_time);
$diff=date_diff($end,$start);
//print_r($diff);
echo $diff->d." days, ".$diff->h." hours, ".$diff->m." minutes, ".$diff->s." seconds";

Result:

3 days, 0 hours, 0 minutes, 0 seconds
devpro
  • 16,184
  • 3
  • 27
  • 38