使用php

时间:2016-04-02 19:59:44

标签: php mysql

我从一个简单的SELECT查询调用一些数据,其中该行包含三个字段start_time(DATETIME)end_time(DATETIME)& reopen_time(DATETIME)

我正在尝试计算start_timeend_time&之间的时间。也在start_time& reopen_time如果是这种情况。我要确定的方法是另一个名为complete的列,它设置值为1或0.如果完整值为1,它将计算start_timeend_time start_timereopen_time的值为0。

现在,我的PHP到目前为止看起来如下;我正在找人帮助套管和眼睛:

$id=$_GET['id']; //this grabs the id from the previous page depending on which task is clicked.
$sql = "SELECT * FROM to_do_list WHERE id=$id";
$result = mysqli_query($db, $sql);

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){    
        $date1 = $row["start_time"];        
        $date2 = $row["end_time"];            
        $diff = abs(strtotime($date2) - strtotime($date1));            
        $years = floor($diff / (365*60*60*24));
        $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
        $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));            
        printf("%d years, %d months, %d days\n", $years, $months, $days);            
    }
} else {
    echo "There are no tasks!";
}

现在,我得到的结果是:0年,0个月,0天,但没有错误。我假设数据库中的数据格式可能不正确?

2 个答案:

答案 0 :(得分:1)

我需要一些时间(见双关语?)看一下PHP类DateTimehttp://php.net/manual/en/class.datetime.php)。

这个类已经有一个内置的方法来获取日期之间的差异,包括闰年等情况,创建一个DataInterval对象。然后使用DataInterval对象,您可以格式化并拉出您想要的部分(月,日,年等......)

while($row = mysqli_fetch_assoc($result)){    
    $date1 = $row["start_time"];        
    $date2 = $row["end_time"];    
    $datetime1 = new DateTime($date1);
    $datetime2 = new DateTime($date2);
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%y years, %m months, %d days');     
 }

注意:如果$date1$date2字段完全相同,那么您将获得报告0的月份,年份等的间隔...因为时间上没有差异。

另外,请注意,因为DateInterval会考虑闰年等变化,所以您需要记住并非每个“月”都相同。如果您需要知道确切的天数差异,DateInterval会提供公共财产days

 $date1 = new DateTime('2015-02-27');  // 2015 was a 'regular' year, Feb had 28 days
 $date2 = new DateTime('2015-03-27');
 $interval_1_2 = $date1->diff($date2);

 $date3 = new DateTime('2016-02-27'); // 2016 was a 'leap' year, Feb had 29 days
 $date4 = new DateTime('2016-03-27');
 $interval_3_4 = $date3->diff($date4);

 echo $interval_1_2->format('%m months, %d day '); // 1 month 0 days 
 echo "Total Days: ".$interval_1_2->days."\n"; // 28 days

 echo $interval_3_4->format('%m months, %d day');  // 1 month 0 days
 echo "Total Days: ".$interval_3_4->days."\n"; // 29 days

开发俱乐部的第一条规则:除非:

,否则不要重新发明轮子
  1. 您不关心功能,只是尝试解决很多很多次问题才能进行某种学习实验。
  2. 并且愿意最终得出一个不完全涵盖100%用例的古怪结果。
  3. 玩得开心。

答案 1 :(得分:1)

您实际上可以使用 TIMESTAMPDIFF

直接在mysql中执行此操作
$id= filter_input(INPUT_GET, 'id',FILTER_VALIDATE_INT); 
//crude sql injection filter, dont use in production

$sql = "
    SELECT *,
    TIMESTAMPDIFF(YEAR, start_time , end_time) as years,
    TIMESTAMPDIFF(MONTH, start_time , end_time) as months,
    TIMESTAMPDIFF(DAY, start_time , end_time) as days
    FROM to_do_list WHERE id=$id
";

$result = mysqli_query($db, $sql);

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){    
        $date1 = $row["start_time"];        
        $date2 = $row["end_time"];            
        $years = $row["years"];        
        $months = $row["months"];        
        $days = $row["days"];        

        printf("%d years, %d months, %d days\n", $years, $months, $days);            
    }
} else {
    echo "There are no tasks!";
}

有关如何正确处理sql注入预防的信息,请参阅 answer