3

I have two timestamps let us say

 $end_date =  2014-09-09 15:03:10 and now date 
 date_default_timezone_set('Asia/Calcutta'); 
 $now = date('Y-m-d H:i:s');

I want to calculate number of days remaining .Suppose if that particular date crosses now date and it should display remaining days with -ve value.

I am using the following code

$remaining_days =strtotime($end_date) - strtotime($now) ;
$Result_days = floor($remaining_days /86400);
echo $remaining_days.'   '.$Result_days.'<br/>' 

Problem is that if the end date = today's date it is displaying -1 . I want to calculate based on time and display remaining days and hours. Please help me to find out the solution.

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Akshobhya
  • 169
  • 2
  • 16
  • @Clement Malet. I have gone through that thread. But it only displays remaining days but not with +ve and -ve offsets – Akshobhya Sep 09 '14 at 10:04
  • I advice you to take a look at the Datetime object.. http://php.net/manual/en/class.datetime.php – Naruto Sep 09 '14 at 10:05
  • @Akshobhya What do you mean by "ve offsets" ? I don't get it – Clément Malet Sep 09 '14 at 10:06
  • I meant see suppose end date = todays date then we have to caluclate based on time and display remaining hours.suppose that time crosees today's time then it should say -1. – Akshobhya Sep 09 '14 at 10:08

3 Answers3

5

Try this:

<?php
$end_date =  "2014-10-09 15:03:10";
date_default_timezone_set('Asia/Calcutta'); 
$now = date('Y-m-d H:i:s');

$diff = strtotime($now) - strtotime($end_date);
$fullDays    = floor($diff/(60*60*24));   
$fullHours   = floor(($diff-($fullDays*60*60*24))/(60*60));   
$fullMinutes = floor(($diff-($fullDays*60*60*24)-($fullHours*60*60))/60);      
echo "Difference is $fullDays days, $fullHours hours and $fullMinutes minutes.";


Output:

Difference is -30 days, 0 hours and 39 minutes.


Demo:

http://3v4l.org/3auqe


Edit (using DATE OBJECT):

<?php

// Example 1
$end_date =  "2014-09-11 20:35:10";
date_default_timezone_set('Asia/Calcutta'); 
$now = date('Y-m-d H:i:s');

$date1=date_create($now);
$date2=date_create($end_date);
$diff=date_diff($date1,$date2,FALSE);
echo $diff->format("%R%d days, %h hours, %m minutes, %s seconds").PHP_EOL;    
//Output:
+2 days, 3 hours, 0 minutes, 44 seconds


// Example 2
$end_date =  "2014-09-08 20:35:10";
date_default_timezone_set('Asia/Calcutta'); 
$now = date('Y-m-d H:i:s');

$date1=date_create($now);
$date2=date_create($end_date);
$diff=date_diff($date1,$date2,FALSE);
echo $diff->format("%R%d days, %h hours, %m minutes, %s seconds").PHP_EOL;    
//Output:
-0 days, 20 hours, 0 minutes, 16 seconds


Demo:

http://3v4l.org/dPSgX#vhhvm-320

Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
2

you can use date_diff php method .you can see example here http://php.net/manual/en/function.date-diff.php

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
0

You may try like this:

<?php   
    $dateDiff    = $date1 - $date2;   
    $fullDays    = floor($dateDiff/(60*60*24));   
    $fullHours   = floor(($dateDiff-($fullDays*60*60*24))/(60*60));   
    $fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);      
    echo "Differernce is $fullDays days, $fullHours hours and $fullMinutes minutes.";   
?>

See the Source for more options

$date1 = new DateTime("2014-09-09");
$date2 = new DateTime("2014-09-12");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";

The duplicate

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331