Finding days between 2 unix timestamps in php

前端 未结 8 1065
一整个雨季
一整个雨季 2020-12-13 18:57

Hay, i have a database holding events. There are 2 fields \'start\' and \'end\', these contain timestamps. When an admin enters these dates, they only have the ability to se

相关标签:
8条回答
  • 2020-12-13 19:13

    You just have to calculate the number of seconds between the two dates, then divide to get days :

    $numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
    

    Then, you can use a for loop to retrieve the dates :

    $numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
    
    for ($i = 1; $i < $numDays; $i++) {
        echo date('Y m d', strtotime("+{$i} day", $smallestTimestamp)) . '<br />';
    }
    

    Again, if you don't know which timestamp is the smallest, you can use the min() function (second argument in strtotime).

    0 讨论(0)
  • 2020-12-13 19:13
    $d1=mktime(22,0,0,1,1,2007);
    $d2=mktime(0,0,0,1,2,2007);
    echo "Hours difference = ".floor(($d2-$d1)/3600) . "<br>";
    echo "Minutes difference = ".floor(($d2-$d1)/60) . "<br>";
    echo "Seconds difference = " .($d2-$d1). "<br>";
    
    
    echo "Month difference = ".floor(($d2-$d1)/2628000) . "<br>";
    echo "Days difference = ".floor(($d2-$d1)/86400) . "<br>";
    echo "Year difference = ".floor(($d2-$d1)/31536000) . "<br>";
    

    http://www.plus2net.com/php_tutorial/date-diff.php

    http://www.phpf1.com/tutorial/php-date-difference.html

    0 讨论(0)
  • 2020-12-13 19:14

    Something like this?

    $day = $start;
    while ($day < $end) {
            $day += 86400;
            echo $day.' '.date('Y-m-d', $day).PHP_EOL;
    }
    

    By the way, 1262304000 is Dec 31, not Jan 1.

    0 讨论(0)
  • 2020-12-13 19:19

    I think that a quick workaround for this is to subtract the amount of a days worth of seconds from the end_stamp until you get to the start_tag.

    //1 day = 86400 seconds
    

    I would build an array of the days to use later.

    EDIT (example)

    $difference = 86400;
    $days = array();
    while ( $start_time < $end_time )
    {
        $days[] = date('M j Y', $end_time);
    
        $end_time -= $difference;
    }
    

    This should cover any time frame even if its over a bunch of months.

    0 讨论(0)
  • 2020-12-13 19:23
        **This is a very simple code for find days hours minutes and seconds in php**
    
        $dbDate = strtotime("".$yourbdDate.""); // Database date
        $endDate = time();    // current time
        $diff = $endDate - $dbDate; /// diffrence
    
        $days = floor($diff/86400);  ///  number of days 
        $hours = floor(($diff-$days*86400)/(60 * 60));  ////  number of hours
        $min = floor(($diff-($days*86400+$hours*3600))/60);///// numbers of minute
    
    
        $second = $diff - ($days*86400+$hours*3600+$min*60); //// secondes
    
        if($days > 0) echo $days." Days ago";
        elseif($hours > 0) echo $hours." Hours ago";
        elseif($min > 0) echo $min." Minute ago";
        else echo "Just second ago";
    
    0 讨论(0)
  • 2020-12-13 19:23

    get the difference of two dates and divide it by 86400. abs(($date1 - $date2) / 86400) will produce the needed result

    0 讨论(0)
提交回复
热议问题