我有一个学校项目,我必须制定议程。我的计算功能有问题。因此,下面的函数需要计算剩余的天数,直到我的下一次约会。我有一个未来的日期($ timefromdb)存储在我的mysql数据库中,我想从我的数据库中计算当前日期和日期之间的差异。
public function getAllAfspraken(){
$db = new Db();
$sql = "SELECT * FROM `tblafspraken`";
$return = $db->conn->query($sql);
while($row = mysqli_fetch_array($return))
{
$currentdate= date("Y-m-d");
$timefromdb = $row['af_datum'];
$timeleft = $currentdate-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo "<h3>".$row['af_datum']."</h3>";
echo "<p>" .$row['af_beschrijving']. "</p>";
echo $daysleft;
echo "<hr />";
}
}
答案 0 :(得分:2)
使用DateTime:diff
进行计算
$date1 = new DateTime("2014-04-30");
$now = new DateTime();
$diff = $date1->diff($now);
echo 'Remaining Days ::'. $diff->days ;
以上将显示+ ve或-ve
您还可以使用$diff->format('%R%a days');
for + ve或-ve
//print_r($diff);
$diff
的完整数据如下所示,可以找到不同参数的差异,如天,小时,分钟等。
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 4
[h] => 10
[i] => 3
[s] => 46
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 1
[days] => 4
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
答案 1 :(得分:1)
我会尝试使用strtotime函数来转换这两个值:
$currentdate= strtotime( date("Y-m-d") );
$timefromdb = strtotime( $row['af_datum'] );
$daysleft = ($timefromdb - $currentdate)/(60 * 60 * 24);
答案 2 :(得分:1)
尝试使用MySQL:
$sql = "SELECT *, DATEDIFF(NOW(), af_datum) AS timeleft FROM `tblafspraken`";
此外,您的代码看起来似乎已经找到了几天,而不是剩余的天数。如果结果不正确,请尝试使用DATEDIFF(af_datum,NOW())。
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
答案 3 :(得分:0)
您可以更进一步,并计算年,月,日,小时等...例如,如果您使用MySQL日期格式,这样的函数可能会起作用:
function get_time_diff($start_date, $end_date = null) {
// make sure start date is in mysql datetime format
$expr = "/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}?$/";
if (!preg_match($expr, $start_date)) {
return false;
}
if (isset($end_date)) {
// make sure end date is in mysql datetime format
if(!preg_match($expr, $end_date)) {
return false;
}
list($date, $time) = explode(' ', $end_date);
list($hour, $minute, $second) = explode(':', $time);
$today = new DateTime($date);
$today->setTime($hour, $minute, $second);
} else {
$today = new DateTime();
}
$target = new DateTime($start_date);
$diff = $today->diff($target);
if ($diff->y > 0) {
echo $diff->y . ' year' . ($diff->y > 1 ? 's' : '') . ($diff->m > 0 ? ', ' : '');
}
if ($diff->m > 0) {
echo $diff->m . ' month' . ($diff->m > 1 ? 's' : '') . ($diff->d > 0 ? ', ' : '');
}
if ($diff->d > 0) {
echo $diff->d . ' day' . ($diff->d > 1 ? 's' : '') . ($diff->h > 0 ? ', ' : '');
}
if ($diff->h > 0) {
echo $diff->h . ' hour' . ($diff->h > 1 ? 's' : '') . ($diff->i > 0 ? ', ' : '');
}
if ($diff->i > 0) {
echo $diff->i . ' minute' . ($diff->i > 1 ? 's' : '') . ($diff->s > 0 ? ', ' : '');
}
if ($diff->s > 0) {
echo $diff->s . ' second' . ($diff->s > 1 ? 's' : '');
}
return $today < $target ? ' until' : ' ago';
}
if ($test = get_time_diff('2012-09-11 10:25:00')) {
echo $test;
} else {
echo 'invalid request';
}
这会输出类似:1年,7个月,14天,5小时,58分钟,6秒之前
由于日期范围有限,你真的不应该使用strtotime()或unix时间戳。