通过mysql获得两个日期的差异

时间:2015-11-22 14:20:47

标签: mysql datediff

我现在无法找到 6 小时的确切答案...... !!

我想按顺序显示生日,首先显示即将到来的生日。

$today_date = date('Y-m-d');

$s = "select * from BDays order by TIMEDIFF(STR_TO_DATE(date,'%Y-%m-%d') , $today_date)";

包含日期的列在标记为“BDays”的表格中标记为“日期”。

在查询中,我试图从今天的日期中减去出生日期以获得即将到来的生日。

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:1)

您希望仅将每个日期/时间的月份和日期与当前月份日期进行比较。有一个功能EXTRACT,你可以在这里做到这一点。像这样:

$s = "select * from BDays
      order by EXTRACT(MONTH_DAY, date) - EXTRACT(MONTH_DAY, $today_date)";

这将返回所有生日,即使已经过去的生日。要只有即将到来的生日,请尝试:

$s = "select * from BDays
       where (EXTRACT(MONTH_DAY, date) - EXTRACT(MONTH_DAY, $today_date)) > 0
       order by EXTRACT(MONTH_DAY, date) - EXTRACT(MONTH_DAY, $today_date)";

答案 1 :(得分:0)

SELECT
    name,
    birthday,
    birthday + INTERVAL(YEAR(CURRENT_TIMESTAMP) - YEAR(birthday)) + 0 YEAR AS currbirthday,
    birthday + INTERVAL(YEAR(CURRENT_TIMESTAMP) - YEAR(birthday)) + 1 YEAR AS nextbirthday
FROM birthdays
ORDER BY CASE
    WHEN currbirthday >= CURRENT_TIMESTAMP THEN currbirthday
    ELSE nextbirthday
END

http://sqlfiddle.com/#!9/9a5c0/1

答案 2 :(得分:0)

你可以试试这个:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>

<div class="container">
  <div class="row">
    <div class="card-deck">
      <div class="card">
        <div class="card-block">
          <h4 class="card-title">iPhone 5S</h4>
          <h6 class="card-subtitle text-muted">Rp. 8.999.999</h6>
        </div>
        <img src="http://placehold.it/380x200" class="img-responsive"/>
        <div class="card-block">
          <p class="card-text">Apple iPhone 5S adalah handphone tercanggih dari Apple. Dapatkan
            dengan harga khas dari Shopoza! Nikmati cicilan 0%
          </p>
          <div class="btn-group">
            <a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Add to cart</a>
            <div class="dropdown-menu">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
            </div>
          </div>
          <a href="#" class="btn btn-secondary card-link">Quick checkout</a>
        </div>
        <!-- card block -->
        <!-- card -->
      </div>
      <div class="card">
        <div class="card-block">
          <h4 class="card-title">iPhone 5S</h4>
          <h6 class="card-subtitle text-muted">Rp. 8.999.999</h6>
        </div>
        <img src="http://placehold.it/380x200" class="img-responsive"/>
        <div class="card-block">
          <p class="card-text">Apple iPhone 5S adalah handphone tercanggih dari Apple. Dapatkan
            dengan harga khas dari Shopoza! Nikmati cicilan 0%
          </p>
          <div class="btn-group">
            <a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Add to cart</a>
            <div class="dropdown-menu">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
            </div>
          </div>
          <a href="#" class="btn btn-secondary card-link">Quick checkout</a>
        </div>
        <!-- card block -->
        <!-- card -->
      </div>
      <div class="card">
        <div class="card-block">
          <h4 class="card-title">iPhone 5S</h4>
          <h6 class="card-subtitle text-muted">Rp. 8.999.999</h6>
        </div>
        <img src="http://placehold.it/380x200" class="img-responsive"/>
        <div class="card-block">
          <p class="card-text">Apple iPhone 5S adalah handphone tercanggih dari Apple. Dapatkan
            dengan harga khas dari Shopoza! Nikmati cicilan 0%
          </p>
          <div class="btn-group">
            <a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Add to cart</a>
            <div class="dropdown-menu">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
            </div>
          </div>
          <a href="#" class="btn btn-secondary card-link">Quick checkout</a>
        </div>
        <!-- card block -->
        <!-- card -->
      </div>
    </div>
  </div>
</div>

结果:

create table BDays (`date` date);
insert into BDays values ('1993-04-01'), ('2015-10-26'), ('1995-08-10'), ('1995-06-05'), ('1994-04-04');

select * from BDays
order by
case 
when `date` > current_date then `date`
else cast(concat(year(`date`)+1, right(`date`, 6)) as date)
end

示例:http://sqlfiddle.com/#!9/d60e2/2

我们在这里按日期订购。如果出生日期尚未到达,请使用该日期。否则采取明年,并将其与月和日结合起来。然后对它进行排序。

在您的日期显示为DD-MM-YYYY,您可以在这些行上使用| date | |---------------------------| | April, 01 1993 00:00:00 | | April, 04 1994 00:00:00 | | June, 05 1995 00:00:00 | | August, 10 1995 00:00:00 | | October, 26 2015 00:00:00 | 或其他内容。

编辑:

cast( concat(left(date, 6), year(date)+1) as date)

结果:

-- added 1994-11-27 to the table
create table BDays (`date` date);
insert into BDays values ('1993-04-01'), ('2015-10-26'), ('1995-08-10'), ('1995-06-05'), ('1994-04-04'), ('1994-11-27');

-- updated query
select * from BDays
order by
case 
when `date` > current_date then `date`
else cast(concat(year(current_date)+1, right(`date`, 6)) as date)
end

SQLFiddle示例:http://sqlfiddle.com/#!9/1f448/13