How can i get minutes in php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we will learn how to get time difference in minutes using PHP. 

    We will be using the built-in function date_diff()toget the time difference in minutes. For this, we will be needed a start date and end date to calculate their time difference in minutes using the date_diff() function.

    Syntax:

    date_diff($datetime1, $datetime2);

    Parameters: The date_diff() function accepts two parameters as mentioned above and described below.

    • $datetime1: This is a mandatory parameter as it specifies the start/first DateTime object.
    • $datetime2: This is a mandatory parameter as it specifies the end/second DateTime object.

    Return Value: This function returns the difference between the first DateTime object and the second DateTime object otherwise it returns false on failure.

    Example 1: The below program illustrates the date_diff() function to get the time difference in minutes.

    PHP

    $dateTimeObject1 = date_create('2019-05-18'); 

    $dateTimeObject2 = date_create('2020-05-18'); 

    $interval = date_diff($dateTimeObject1, $dateTimeObject2); 

    echo ("Difference in days is: ");

    echo $interval->format('%R%a days');

    echo "\n
    "
    ;

    $min = $interval->days * 24 * 60;

    $min += $interval->h * 60;

    $min += $interval->i;

    echo("Difference in minutes is: ");

    echo $min.' minutes';

    ?>

    Output:

    Difference in days is: +366 days
    Difference in minutes is: 527040 minutes

    Example 2:

    PHP

      $dateTimeObject1 = date_create('2020-05-14'); 

      $dateTimeObject2 = date_create('2021-02-14'); 

      $interval = date_diff($dateTimeObject1, $dateTimeObject2); 

      echo ("Difference in days is: ");

      echo $interval->format('%R%a days');

      echo "\n
    "
    ;

      $min = $interval->days * 24 * 60;

      $min += $interval->h * 60;

      $min += $interval->i;

      echo("Difference in minutes is: ");

      echo $min.' minutes';

    ?>

    Output:

    Difference in days is: +276 days
    Difference in minutes is: 397440 minutes

    Example 3:

    PHP

    $dateTimeObject1 = date_create('19:15:00'); 

    $dateTimeObject2 = date_create('12:15:00'); 

    $interval = date_diff($dateTimeObject1, $dateTimeObject2); 

    echo ("Difference in hours is:");

    echo $interval->h;

    echo "\n
    "
    ;

    $minutes = $interval->days * 24 * 60;

    $minutes += $interval->h * 60;

    $minutes += $interval->i;

    echo("Difference in minutes is:");

    echo $minutes.' minutes';

    ?>

    Output:

    Difference in hours is:7
    Difference in minutes is:420 minutes

    How can I get hours and minutes in php?

    Converting minutes to hours and minutes with PHP.
    if ($time < 1) {.
    return;.
    $hours = floor($time / 60);.
    $minutes = ($time % 60);.
    return sprintf($format, $hours, $minutes);.

    How to Add minutes to time() php?

    For this, you can use the strtotime() method. $anyVariableName= strtotime('anyDateValue + X minute'); You can put the integer value in place of X.

    How can I get minutes between two dates in php?

    php $start = strtotime('12:01:00'); $end = strtotime('13:16:00'); $mins = ($end - $start) / 60; echo $mins; ?>

    How to Add time in current time in php?

    Example 1: PHP Add Hours to DateTime.
    index.php. $date = "2021-11-01 10:10:05"; $newDate = date('Y-m-d H:i:s', strtotime($date. ... .
    Output: 2021-11-01 13:10:05..
    index.php. $newDate = date('Y-m-d H:i:s', strtotime(' + 3 hours')); echo $newDate; ... .
    Output: Read Also: PHP Subtract Year from Date Example. 2021-11-10 06:49:33..