2012-05-11 69 views
1

我有一个mySQL数据库。计算两个日期之间的天数

我需要计算两个日期之间的天数。

我的客户将通过php表格填写输入hm_date与1979年1月1日创建一个新的记录。

我需要一个字段total_days来计算从hm_date到当天的总天数。我需要这个领域随时更新自己。

我该如何让hm_date与总天数一起出现并始终更新?

我asume这可以实现服务器端?

我应该用strototime()

回答

8

你需要使用MySQL的DATEDIFF()

DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30'); 
     -> 1 
mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31'); 
     -> -31 

根据您的问题,我想你会想DATE_DIFF(hm_date, CURRENT_DATE)。只要确保hm_date的格式为YYYY-MM-DD即可。

+0

是否有一个例子的任何地方的语法和如何写它? – Erik

+0

查看我的更新回答 –

+0

是对的吗? mysql> SELECT DATEDIFF('hm_date'); - > 1 mysql> SELECT DATEDIFF('total_days'); - > -31 – Erik

1

随着PHP:

$daydiff = floor((strtotime($endDate) - strtotime($startDate))/86400); 

$的startDate和结束日期$可以是任何有效的日期格式说明如下: http://www.php.net/manual/en/datetime.formats.date.php

+3

一天并不总是24小时。在DST期间,它可能是23或25个。因此,这有时会失败。 – Corbin

+0

“天差”的概念不能在没有假定一天24小时的情况下以编程方式计算。政府可以为dst切换设定一个日期。再次,它不能100%准确的日期,没有输入实际时间。 –

+1

在这方面的错误是,一天* *定义良好。这只是不方便*定义。夏令时是一个婊子,但你必须忍受的婊子。在夏令时出现错误并不是一个愉快的体验,尤其是当您查看代码并转到“Awww ...我认为它是24小时。” – Corbin

1

它很容易,但长。请遵循以下代码

<?php 

    // Set timezone 
    date_default_timezone_set("UTC"); 

    // Time format is UNIX timestamp or 
    // PHP strtotime compatible strings 
    function dateDiff($time1, $time2, $precision = 6) { 
    // If not numeric then convert texts to unix timestamps 
    if (!is_int($time1)) { 
     $time1 = strtotime($time1); 
    } 
    if (!is_int($time2)) { 
     $time2 = strtotime($time2); 
    } 

    // If time1 is bigger than time2 
    // Then swap time1 and time2 
    if ($time1 > $time2) { 
     $ttime = $time1; 
     $time1 = $time2; 
     $time2 = $ttime; 
    } 

    // Set up intervals and diffs arrays 
    $intervals = array('year','month','day','hour','minute','second'); 
    $diffs = array(); 

    // Loop thru all intervals 
    foreach ($intervals as $interval) { 
     // Set default diff to 0 
     $diffs[$interval] = 0; 
     // Create temp time from time1 and interval 
     $ttime = strtotime("+1 " . $interval, $time1); 
     // Loop until temp time is smaller than time2 
     while ($time2 >= $ttime) { 
    $time1 = $ttime; 
    $diffs[$interval]++; 
    // Create new temp time from time1 and interval 
    $ttime = strtotime("+1 " . $interval, $time1); 
     } 
    } 

    $count = 0; 
    $times = array(); 
    // Loop thru all diffs 
    foreach ($diffs as $interval => $value) { 
     // Break if we have needed precission 
     if ($count >= $precision) { 
    break; 
     } 
     // Add value and interval 
     // if value is bigger than 0 
     if ($value > 0) { 
    // Add s if value is not 1 
    if ($value != 1) { 
     $interval .= "s"; 
    } 
    // Add value and interval to times array 
    $times[] = $value . " " . $interval; 
    $count++; 
     } 
    } 

    // Return string with times 
    return implode(", ", $times); 
    } 

?> 

现在尝试一下,看看它是如何显示差异...

echo dateDiff("2010-01-26", "2004-01-26") . "\n"; 
echo dateDiff("2006-04-12 12:30:00", "1987-04-12 12:30:01") . "\n"; 
echo dateDiff("now", "now +2 months") . "\n"; 
echo dateDiff("now", "now -6 year -2 months -10 days") . "\n"; 
echo dateDiff("2009-01-26", "2004-01-26 15:38:11") . "\n"; 
相关问题