How to get time difference in minutes in PHP

后端 未结 17 2239
独厮守ぢ
独厮守ぢ 2020-11-21 07:22

How to calculate minute difference between two date-times in PHP?

相关标签:
17条回答
  • 2020-11-21 07:56
    $date1=date_create("2020-03-15");
    $date2=date_create("2020-12-12");
    $diff=date_diff($date1,$date2);
    echo $diff->format("%R%a days");
    

    For detailed format specifiers, visit the link.

    0 讨论(0)
  • 2020-11-21 08:01

    The answers above are for older versions of PHP. Use the DateTime class to do any date calculations now that PHP 5.3 is the norm. Eg.

    $start_date = new DateTime('2007-09-01 04:10:58');
    $since_start = $start_date->diff(new DateTime('2012-09-11 10:25:00'));
    echo $since_start->days.' days total<br>';
    echo $since_start->y.' years<br>';
    echo $since_start->m.' months<br>';
    echo $since_start->d.' days<br>';
    echo $since_start->h.' hours<br>';
    echo $since_start->i.' minutes<br>';
    echo $since_start->s.' seconds<br>';
    

    $since_start is a DateInterval object. Note that the days property is available (because we used the diff method of the DateTime class to generate the DateInterval object).

    The above code will output:

    1837 days total
    5 years
    0 months
    10 days
    6 hours
    14 minutes
    2 seconds

    To get the total number of minutes:

    $minutes = $since_start->days * 24 * 60;
    $minutes += $since_start->h * 60;
    $minutes += $since_start->i;
    echo $minutes.' minutes';
    

    This will output:

    2645654 minutes

    Which is the actual number of minutes that has passed between the two dates. The DateTime class will take daylight saving (depending on timezone) into account where the "old way" won't. Read the manual about Date and Time http://www.php.net/manual/en/book.datetime.php

    0 讨论(0)
  • 2020-11-21 08:01

    Subtract the times and divide by 60.

    Here is an example which calculate elapsed time from 2019/02/01 10:23:45 in minutes:

    $diff_time=(strtotime(date("Y/m/d H:i:s"))-strtotime("2019/02/01 10:23:45"))/60;
    
    0 讨论(0)
  • 2020-11-21 08:02

    I wrote this function for one my blog site(difference between a past date and server's date). It will give you an output like this

    "49 seconds ago", "20 minutes ago", "21 hours ago" and so on

    I have used a function that would get me the difference between the date passed and the server's date.

    <?php
    
    //Code written by purpledesign.in Jan 2014
    function dateDiff($date)
    {
        $mydate= date("Y-m-d H:i:s");
        $theDiff="";
        //echo $mydate;//2014-06-06 21:35:55
        $datetime1 = date_create($date);
        $datetime2 = date_create($mydate);
        $interval = date_diff($datetime1, $datetime2);
        //echo $interval->format('%s Seconds %i Minutes %h Hours %d days %m Months %y Year    Ago')."<br>";
        $min=$interval->format('%i');
        $sec=$interval->format('%s');
        $hour=$interval->format('%h');
        $mon=$interval->format('%m');
        $day=$interval->format('%d');
        $year=$interval->format('%y');
        if($interval->format('%i%h%d%m%y')=="00000") {
            //echo $interval->format('%i%h%d%m%y')."<br>";
            return $sec." Seconds";
        } else if($interval->format('%h%d%m%y')=="0000"){
            return $min." Minutes";
        } else if($interval->format('%d%m%y')=="000"){
            return $hour." Hours";
        } else if($interval->format('%m%y')=="00"){
            return $day." Days";
        } else if($interval->format('%y')=="0"){
            return $mon." Months";
        } else{
            return $year." Years";
        }    
    }
    ?>
    

    Save it as a file suppose "date.php". Call the function from another page like this

    <?php
     require('date.php');
     $mydate='2014-11-14 21:35:55';
     echo "The Difference between the server's date and $mydate is:<br> ";
     echo dateDiff($mydate);
    ?>
    

    Of course you can modify the function to pass two values.

    0 讨论(0)
  • 2020-11-21 08:02

    I found so many solution but I never got correct solution. But i have created some code to find minutes please check it.

    <?php
    
      $time1 = "23:58";
      $time2 = "01:00";
      $time1 = explode(':',$time1);
      $time2 = explode(':',$time2);
      $hours1 = $time1[0];
      $hours2 = $time2[0];
      $mins1 = $time1[1];
      $mins2 = $time2[1];
      $hours = $hours2 - $hours1;
      $mins = 0;
      if($hours < 0)
      {
        $hours = 24 + $hours;
      }
      if($mins2 >= $mins1) {
            $mins = $mins2 - $mins1;
        }
        else {
          $mins = ($mins2 + 60) - $mins1;
          $hours--;
        }
        if($mins < 9)
        {
          $mins = str_pad($mins, 2, '0', STR_PAD_LEFT);
        }
        if($hours < 9)
        {
          $hours =str_pad($hours, 2, '0', STR_PAD_LEFT);
        }
    echo $hours.':'.$mins;
    ?>
    

    It gives output in hours and minutes for example 01 hour 02 minutes like 01:02

    0 讨论(0)
  • 2020-11-21 08:04

    This will help....

    function get_time($date,$nosuffix=''){
        $datetime = new DateTime($date);
        $interval = date_create('now')->diff( $datetime );
        if(empty($nosuffix))$suffix = ( $interval->invert ? ' ago' : '' );
        else $suffix='';
        //return $interval->y;
        if($interval->y >=1)        {$count = date(VDATE, strtotime($date)); $text = '';}
        elseif($interval->m >=1)    {$count = date('M d', strtotime($date)); $text = '';}
        elseif($interval->d >=1)    {$count = $interval->d; $text = 'day';} 
        elseif($interval->h >=1)    {$count = $interval->h; $text = 'hour';}
        elseif($interval->i >=1)    {$count = $interval->i; $text = 'minute';}
        elseif($interval->s ==0)    {$count = 'Just Now'; $text = '';}
        else                        {$count = $interval->s; $text = 'second';}
        if(empty($text)) return '<i class="fa fa-clock-o"></i> '.$count;
        return '<i class="fa fa-clock-o"></i> '.$count.(($count ==1)?(" $text"):(" ${text}s")).' '.$suffix;     
    }
    
    0 讨论(0)
提交回复
热议问题