使用date()方法计算日期差 [英] Calculating Date Difference using date() method

查看:516
本文介绍了使用date()方法计算日期差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试执行以下代码来计算使用PHP $date = date('Y-m-d H:i:s');函数获得的日期之间的日期差.

I've been trying to execute the following piece of code to calculate the difference b/w the dates obtained using the PHP $date = date('Y-m-d H:i:s'); function.

$fine = mysqli_query($conn, "SELECT DATEDIFF(".$query_exec['Date_issued'].", ".$query_exec['Date_returned'].") AS days");
$fine = $fine*10;

即使日期之间的日期差> 0,返回的$ fine值也仅为0.

The value that $fine if returning is only 0 even if the difference b/w the dates is >0.

请帮助,谢谢

推荐答案

我怀疑$query_exec['Date_issued']$query_exec['Date_returned']包含类似2017-03-012017-03-30的东西,当它们插入上述SQL代码时会产生:

I suspect $query_exec['Date_issued'] and $query_exec['Date_returned'] contain something like 2017-03-01, 2017-03-30 that, when inserted in the SQL code above produces:

SELECT DATEDIFF(2017-03-30, 2017-03-01) AS DAYS

此SQL中没有DATE值. 2017-03-012017-03-30是数值表达式,其结果为20131984.

There is no DATE value in this SQL. 2017-03-01 and 2017-03-30 are numeric expressions that evaluates to 2013 and 1984.

在SQL中用引号将$query_exec['Date_issued']$query_exec['Date_returned']的值括起来:

Enclose the values of $query_exec['Date_issued'] and $query_exec['Date_returned'] in quotes in the SQL:

$sql = sprintf("SELECT DATEDIFF('%s','%s') AS DAYS",
               $query_exec['Date_issued'], $query_exec['Date_returned']);

我使用 sprintf() 使其更具可读性.如果您更喜欢,也可以使用字符串串联.

I used sprintf() to make it more readable. You can use strings concatenation as well if you like it more.

这篇关于使用date()方法计算日期差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆