计算两个给定日期之间的天数 [英] Calculate number of days between two given dates

查看:138
本文介绍了计算两个给定日期之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在脚本中更正错误,以计算2个日期之间的天数。
日期已通过表单输入,变量信息如下:

Can anyone correct the error in my script to calculate the number of days between 2 dates. The dates have been input through a form, the variable info is as followed:

[departon] => Array ( [0] => 1 [1] => June [2] => 2011 )     
[returnon] => Array ( [0] => 31 [1] => June [2] => 2011 ) 

我已经编写了代码来计算这些日期,但不计算日期,它只输出0。

I have written the code to calculate these dates, but its not calculating the day, it just outputs 0.

<?php
$first_date = mktime(12, 0, 0, $_POST['departon'][1], $_POST['departon'][0], $_POST['departon'][2]); 
$second_date = mktime(12, 0, 0, $_POST['returnon'][1], $_POST['returnon'][0], $_POST['returnon'][2]);
$days = $second_date-$first_date;
echo floor($days/60/60/24) . " days"; 
?>

帮助将不胜感激。

推荐答案

这是一个简单的方法:

$depart = strtotime(implode(' ', $_POST['departon']));
$return = strtotime(implode(' ', $_POST['returnon']));

$diff = floor(($return - $depart) / (60 * 60 * 24));

注意:6月份只有30天。

Note: there's only 30 days in June.

这篇关于计算两个给定日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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