0

I have 2 datetime , and I wanna to calculate difference in minutes :

 $date_reservation=strtotime($_POST['date_reservation']);;
$dt = new DateTime();
$date_annulation =  strtotime($dt->format('Y-m-d H:i:s'));
$attente = (round(abs($date_annulation - $date_reservation)/60)%60);

It only take the difference between minute without hours .

I've tried this function ( from php documentation )

$interval = $date_annulation->diff($date_reservation); 
$years = $interval->format('%y'); 
$months = $interval->format('%m'); 
$days = $interval->format('%d'); 

But it dosn't work (error 500)

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
user3070123
  • 159
  • 2
  • 20
  • Possible duplicate of [How to calculate the difference of datetime field and now in PHP?](http://stackoverflow.com/questions/10448119/how-to-calculate-the-difference-of-datetime-field-and-now-in-php) – Murad Hasan May 20 '16 at 12:03
  • `$date_annulation` is not a `DateTime` object, but an Unix timestamp (int) - therefor the server error – DerVO May 20 '16 at 12:08

4 Answers4

0

If you are aware of the possible inaccuracies using this approach, you can just compare the Unix timestamps:

$date_reservation = strtotime('2016-05-20 13:30');
$date_annulation = strtotime('now');
$diff_minutes = round(abs($date_annulation - $date_reservation)/60, 0);

The only problem in your code is, that you mod 60 your minutes (%60). This of course gives you only the minutes above full hours.

Please note that strtotime interprets the datetime string using the time zone set on the machine running this code. You can set in your code using date_default_timezone_set().

See an example on ideone.com.

DerVO
  • 3,679
  • 1
  • 23
  • 27
0

Your problem is that $date_annulation is not a DateTime object. Keep it simple using strtotime

<?php
$date_reservation = strtotime($_POST['date_reservation']);
$date_annulation = strtotime('now');
$diff_minutes = ($date_annulation - $date_reservation)/60;

echo $diff_minutes;
ecorvo
  • 3,559
  • 4
  • 24
  • 35
0

A response of 500 Internal Server Error means there is a problem in your PHP code. Check the server log files (Apache's error_log, PHP's php_errors.log) to find out the exact place (file and line) and the cause.

In the meantime, the code you copied from the documentation doesn't work because you tried to call a method of the DateTime class on a number (the value returned by strtotime(). It doesn't work this way.

It does work, however, if you use DateTime (and related) objects:

$date_reservation = new DateTime($_POST['date_reservation']);
$date_annulation  = new DateTime('now');

$interval = $date_annulation->diff($date_reservation); 
$years = $interval->format('%y'); 
$months = $interval->format('%m'); 
$days = $interval->format('%d'); 
axiac
  • 68,258
  • 9
  • 99
  • 134
-1

i give you code of time difference

<?php 
//$datetime1=strtotime('Y-m-d','time to which difference obtained');
$current_time =strtotime(date("Y-m-d H:i:s"));
//$checkTimeEnd = strtotime('time to which difference obtained');
    $checkTimeStart = strtotime('time to which difference obtained');
    //echo $current_time;
    $all = round((($current_time - $checkTimeStart) / 60),2);
    //echo floor($all/(60*60*24));

    $test = round($all/60,2);

    $d = floor ($all / 1440);
    $h = floor (($all - $d * 1440) / 60);
    $m = $all - ($d * 1440) - ($h * 60);

?>

print $d for date $h for hours $m for minutes

user5876330
  • 79
  • 1
  • 9
  • I've test it , it doesn't help me :it gives false value :float(0)($d) float(0)($h) float(0)($m) "attente":64.97, – user3070123 May 20 '16 at 13:08