-3

Im looking at a simple way to get the number of minutes past since a datetime. Ive tried multiple ideas, but the format is different from what it is expected, its dd/mm/yyyy hh:mm:ss (eg 18/05/2018 15:00:11). I tried converting the string to a date into the correct format, but it keeps failing.

Any ideas?

ItsPronounced
  • 5,475
  • 13
  • 47
  • 86
  • 1
    possible duplicate of https://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php – Laura Vieira May 18 '18 at 15:15
  • 1
    `DateTime::createFromFormat('d/m/Y H:i:s', $yourValue)` – CD001 May 18 '18 at 15:26
  • 2
    Possible duplicate of [How to get time difference in minutes in PHP](https://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – iff_or May 18 '18 at 16:43

3 Answers3

0

When I do the following code:

<?php
  echo strtotime(str_replace("/", "-", "18/05/2018 15:00:11");
?>

I get:

1526670011

This way, try to use the normal syntax of date and time and get two times this way:

<?php
  echo strtotime(str_replace("/", "-", "18/05/2018 14:00:00"));
  echo "<br />";
  echo strtotime(str_replace("/", "-", "18/05/2018 15:00:00"));
  echo "<br />";
  echo strtotime(str_replace("/", "-", "18/05/2018 15:00:00")) - strtotime(str_replace("/", "-", "18/05/2018 14:00:00"));
?>

You get in seconds.

1526666400
1526670000
3600

Convert them into minutes by dividing it by 60.

<?php
  $start = "18/05/2018 14:00:00";
  $end   = "18/05/2018 15:00:00";

  echo strtotime(str_replace("/", "-", $start));
  echo "<br />";
  echo strtotime(str_replace("/", "-", $end));
  echo "<br />";
  echo strtotime(str_replace("/", "-", $end)) - strtotime(str_replace("/", "-", $start)) . " seconds";
  echo "<br />";
  echo (strtotime(str_replace("/", "-", $end)) - strtotime(str_replace("/", "-", $start)))/60 . " minutes";

You get something like:

1526666400
1526670000
3600 seconds
60 minutes

Hope this helps.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Look into Carbon, it will be the last time api extension you'll ever need. you can use the ->diffForHumans(); option and tons of other things that will make your life so much easier if your dealing with times and dates a lot.

ATechGuy
  • 1,240
  • 8
  • 13
0

Just use DateTime::createFromFormat():

As a one-liner:

echo floor(((new DateTime())->getTimestamp() - DateTime::createFromFormat('d/m/Y H:i:s', '18/05/2018 15:00:11')->getTimestamp()) / 60);


Or as something that's a little easier to understand - with comments:

// your timestring in d/m/Y H:i:s format
$myTimeString = '18/05/2018 15:00:11';

// convert your timestring to a DateTime object
$myDateTime = DateTime::createFromFormat('d/m/Y H:i:s', $myTimeString);

// create a DateTime object for "now"
$now = new DateTime();

// use the timestamps to create a difference, in seconds
// between now and your DateTime
$diff = $now->getTimestamp() - $myDateTime->getTimestamp();

// convert it to whole minutes (rounded down) and echo out
echo floor($diff / 60);


You could also use DateTime::diff() but that would give you a DateInterval object representing the difference rather than just the total number of minutes.

CD001
  • 8,332
  • 3
  • 24
  • 28