php - mysql+php 减去两次并显示差值

标签 php datetime mysql

我必须显示mysql中的列(reg_time)与当前时间之间的差异,以便最终输出(在一些php之后以分钟或秒或小时或天显示) 是:

注册:8 小时前

有什么函数可以做到这一点吗? 谢谢

最佳答案

//End date is current date
//$start_date, lets say sep 05 82 : 00:00:00

$seconds_dif = mktime(date("G"),date("i"),date("s"),date("m"),date("d"),date("Y")) - mktime('00','00','00','09','05','1982');

//Difference in seconds is $seconds_dif

//Now only math calculation to figure out months/years..

echo '<br />Years Difference   = '. floor($seconds_dif/365/60/60/24);
echo '<br />Months Difference  = '. floor($seconds_dif/60/60/24/7/4);
echo '<br />Weeks Difference   = '. floor($seconds_dif/60/60/24/7);
echo '<br />Days Difference    = '. floor($seconds_dif/60/60/24);
echo '<br />Hours Difference   = '. floor($seconds_dif/60/60);
echo '<br />Minutes Difference = '. floor($seconds_dif/60);

在这里找到:http://www.webhostingtalk.com/showthread.php?t=453668

您需要根据您的数据格式编辑第三行代码: mktime('00','00','00','09','05','1982')

您还需要将此代码包装到一个函数中以便于使用它:

/**
 * Return string with date time difference between two arguments
 * @param $start_date integer Start date in unix time (seconds from January 1 1970), you can use strtotime('24 November 2003 13:45:54'), for example
 * @param $end_date integer [optional] End date in unix time, by default it equals now
 * @return string
 */
function getDateTimeDifference($start_date, $end_date = time()) {
    $end_date = mktime(date("G", $end_date),date("i", $end_date),date("s", $end_date),date("m", $end_date),date("d", $end_date),date("Y", $end_date));
    $difference = $end_date - mktime(date("G", $start_date),date("i", $start_date),date("s", $start_date),date("m", $start_date),date("d", $start_date),date("Y", $start_date));
    $years = floor($seconds_dif/365/60/60/24);
    $months = floor($seconds_dif/60/60/24/7/4);
    $weeks = floor($seconds_dif/60/60/24/7);
    $days = floor($seconds_dif/60/60/24);
    $hours = floor($seconds_dif/60/60);
    $minutes = floor($seconds_dif/60);

    $ret = 'Difference: ';
    if (!empty($years))
        $ret .= $years . ' years, ';
    if (!empty($months))
        $ret .= $months . ' months, ';
    if (!empty($weeks))
        $ret .= $weeks . ' weeks, ';
    if (!empty($days))
        $ret .= $days . ' days, ';
    if (!empty($hours))
        $ret .= $hours . ' hours, ';
    if (!empty($minutes))
        $ret .= $minutes . 'minutes';

    return $ret;
}

关于php - mysql+php 减去两次并显示差值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1893080/

相关文章:

datetime - Go中灵活的日期/时间解析(在解析中添加默认值)

mysql - Rails 时区 - 本地主机和生产服务器之间的结果不同

php - 通过 PHPmailer 发送 HTML 表格

php - 360 度网格——以奇数条记录结束行

php - 如何限制SQL Server中的查询执行时间?

javascript - 验证时间范围内

php - Amazon AMI(EC2)上的PHP + SQLite3

javascript - 将日期从变量转换为 MM-DD-YYYY

mysql - Ruby Mysql 无法连接到本地主机 (10061)

php - 我的查询中的 MYSQL 排序列