2014-04-25 161 views
0

我有一个学校项目,我必须提出议程。我的计算功能有问题。所以下面的功能需要计算剩余的日子,直到我的下一个约会。我有一个未来的日期($ timefromdb)存储在我的MySQL数据库中,我想计算当前日期和我的数据库中的日期之间的差异。计算剩余天数

public function getAllAfspraken(){ 
    $db = new Db(); 
    $sql = "SELECT * FROM `tblafspraken`"; 
    $return = $db->conn->query($sql); 

     while($row = mysqli_fetch_array($return)) 
     { 
     $currentdate= date("Y-m-d"); 
     $timefromdb = $row['af_datum']; 
     $timeleft = $currentdate-$timefromdb; 
     $daysleft = round((($timeleft/24)/60)/60); 
     echo "<h3>".$row['af_datum']."</h3>"; 
     echo "<p>" .$row['af_beschrijving']. "</p>"; 
     echo $daysleft; 
     echo "<hr />"; 
     } 
    } 
+0

感谢您的帮助,我已经找到了解决方案。当函数进行计算时,我已将时间戳放在$ currentdate和$ timefromdb之前。 –

回答

2

使用DateTime:diff为计算

$date1 = new DateTime("2014-04-30"); 
$now = new DateTime(); 

$diff = $date1->diff($now); 

echo 'Remaining Days ::'. $diff->days ; 

上面会显示+已经或-ve

您还可以使用$diff->format('%R%a days'); 为+已经或-ve

//print_r($diff); 

完整数据为$diff如下所示能找到像天,小时,分钟等不同的参数差异

DateInterval Object 
(
    [y] => 0 
    [m] => 0 
    [d] => 4 
    [h] => 10 
    [i] => 3 
    [s] => 46 
    [weekday] => 0 
    [weekday_behavior] => 0 
    [first_last_day_of] => 0 
    [invert] => 1 
    [days] => 4 
    [special_type] => 0 
    [special_amount] => 0 
    [have_weekday_relative] => 0 
    [have_special_relative] => 0 
) 
1

我会尝试使用的strtotime功能都值转换是这样的:

$currentdate= strtotime(date("Y-m-d")); 
    $timefromdb = strtotime($row['af_datum']); 
    $daysleft = ($timefromdb - $currentdate)/(60 * 60 * 24); 

http://www.php.net/manual/en/function.strtotime.php

0

如果你使用MySQL的日期格式你可以一步借此,并计算年,月,日,小时,等...例如,像这样的功能可能工作:

function get_time_diff($start_date, $end_date = null) { 
    // make sure start date is in mysql datetime format 
    $expr = "/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}?$/"; 
    if (!preg_match($expr, $start_date)) { 
     return false; 
    } 

    if (isset($end_date)) { 
     // make sure end date is in mysql datetime format 
     if(!preg_match($expr, $end_date)) { 
      return false; 
     } 

     list($date, $time) = explode(' ', $end_date); 
     list($hour, $minute, $second) = explode(':', $time); 
     $today = new DateTime($date); 
     $today->setTime($hour, $minute, $second); 
    } else { 
     $today = new DateTime(); 
    } 
    $target = new DateTime($start_date); 
    $diff = $today->diff($target); 

    if ($diff->y > 0) { 
     echo $diff->y . ' year' . ($diff->y > 1 ? 's' : '') . ($diff->m > 0 ? ', ' : ''); 
    } 
    if ($diff->m > 0) { 
     echo $diff->m . ' month' . ($diff->m > 1 ? 's' : '') . ($diff->d > 0 ? ', ' : ''); 
    } 
    if ($diff->d > 0) { 
     echo $diff->d . ' day' . ($diff->d > 1 ? 's' : '') . ($diff->h > 0 ? ', ' : ''); 
    } 
    if ($diff->h > 0) { 
     echo $diff->h . ' hour' . ($diff->h > 1 ? 's' : '') . ($diff->i > 0 ? ', ' : ''); 
    } 
    if ($diff->i > 0) { 
     echo $diff->i . ' minute' . ($diff->i > 1 ? 's' : '') . ($diff->s > 0 ? ', ' : ''); 
    } 
    if ($diff->s > 0) { 
     echo $diff->s . ' second' . ($diff->s > 1 ? 's' : ''); 
    } 
    return $today < $target ? ' until' : ' ago'; 
} 

if ($test = get_time_diff('2012-09-11 10:25:00')) { 
    echo $test; 
} else { 
    echo 'invalid request'; 
} 

这将输出类似:1年7个月,14天5个小时58分6秒前

你真的不应该使用的strtotime()或UNIX由于其时间戳日期范围有限。