Return hours and minutes apart with PHP

Asked

Viewed 2,732 times

4

I have this simple file:

php test.

  <?php

  $date1='2018-01-09 16:14:01';
  $date2='2018-01-09 17:30:04';

  $dateS1 = new \DateTime($date1);
  $dateS2 = new \DateTime($date2);

  $dateDiff = $dateS1->diff($dateS2);
  $result = $dateDiff->h . ' horas e ' . $dateDiff->i . ' minutos';
  echo $result;
  ?>

In theory I should return the difference in hours and minutes, but I turn only a blank page.

Do you think it might be the PHP version on my machine? I use version 5.1.6

  • this code is right, has been tested and works normally, if you can post the error that appears,

  • That’s the problem kkk, returns nothing, just a blank page

  • Here it worked perfectly. Are the two files in the same folder? the file funcoes.php has the <?php and <?php tags ?>?

  • You could pass all the content of.php functions?

  • @Rafaelweber, yes all the files are in a single folder, and tb are with the tags, I will edit the question with the code in a single file to facilitate, because try together and tb did not work

  • 2

    no errors, see http://kithomepage.com/sos/paginainclud.php

  • According to the PHP Documentation the version must be greater than or equal to 5.3 http://php.net/manual/en/datetime.diff.php

  • @Rafaelweber, puts, so this is it :(, I’m on the company server, there’s another way to make this difference calculation?

  • I ran this code in version 5.1.6 of php and it gave fatal error, date time class not found

  • Test on this site, you have all versions of php to test, this is version 5.3 http://sandbox.onlinephpfunctions.com/code/6e4b72504cc1454f009e4acaf17c912ace3ded25

  • And this is version 5.1.6 http://sandbox.onlinephpfunctions.com/code/8a71b4c79f70e95507984388c01624e563a52c70

Show 6 more comments

3 answers

8


According to the PHP Documentation the version must be greater than or equal to 5.3

PHP Datetime::diff

For your version I found a Question already answered that can help you.

  • But this method only works with hours, I need one that counts the days too :(

3

I replicated your code on this one website which contains the php server with the 5.1.6 and it presents a fatal error pois a classe nao está definida.

and as has been said in the php documentation the class datetime.diff is valid for php 5.3 or higher

  • I did it, man, but it didn’t work, and I use other functions with parentheses and that never gave problem :(

  • guy replicated your codes in a test environment and putting the recommendation of the php tag in the file funcoes.php does not present any error, in case the problem is not this is something you need to add in the question, that Voce did not add

  • @Shinchila_matadora what I could complement so that the answer is given as accepted ?

  • The problem is only in the version used, see that the question has been edited and no longer has the include and still presents error due to version

  • @Leocaracciolo was worth :D

2

As already answered, the class DateTime is available in versions PHP 5 >= 5.2.0 e PHP 7.

What you can do to get around it is:

$data1 = '2018-01-09 16:14:01';
$data2 = '2018-01-09 17:30:04';

$unix_data1 = strtotime($data1);
$unix_data2 = strtotime($data2);

$nHoras   = ($unix_data2 - $unix_data1) / 3600;
$nMinutos = (($unix_data2 - $unix_data1) % 3600) / 60;

printf('%02d:%02d', $nHoras, $nMinutos); // 01:16

Example in ideone

  • And if I wanted to calculate the difference of days, month or year?

  • And how I save it in the bank as a string, in case the result 01:16?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.