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?
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?
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";
- 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");
$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
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