PHP计算两个日期之间的天数
我正在开发一个围绕日期的网络应用程序。
我需要根据经过的天数来计算数字,例如 - 伪代码
$count_only = array('monday', 'wednesday', 'friday'); //count only these days
$start_date = 1298572294; // a day in the past
$finish_date = 1314210695; //another day
$var = number_of_days_between($start_date, $finish_date, $count_only);
有没有办法确定已经过去了多少整天,同时只计算某些天数?
I am developing a web application which revolves around dates.
I need to calculate numbers based around days elasped, for example - pseudo code
$count_only = array('monday', 'wednesday', 'friday'); //count only these days
$start_date = 1298572294; // a day in the past
$finish_date = 1314210695; //another day
$var = number_of_days_between($start_date, $finish_date, $count_only);
Is there a way determine how many full days have elapsed, while only counting certain days?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过计算两个指定日期之间有多少个完整周来大大简化这一过程,然后对开始/结束部分周进行一些数学计算以考虑悬空日期。
例如
,那么只需检查悬空日期即可:
You can simplify this considerably by calculating how many complete weeks fall between the two specified dates, then do some math for the beginning/end partial weeks to account for dangling dates.
e.g.
Then it's just a simple matter of checking for the dangling dates:
您可以创建一个循环,从
$start_date
开始转到$count_only
数组中的第二天,并在到达$ 时停止(从函数返回)结束日期
。You could create a loop which goes to the next day in the
$count_only
array, from the$start_date
and stopping (returning from the function) upon reaching the$end_date
.当然有办法:-)
过去的日子根本
就得不到你需要的结果。你可以做的是以下(伪)代码:
我做什么:
我迭代两个日期之间的每一天,用 date('l') 获取日期名称;并检查它是否在数组内。
可能需要进行一些微调,但这应该可以让您继续前进。
Of course there is a way :-)
The days that have been elapsed is simply
This will not get the result you need. What you could do is the following (pesudo)code:
What I do:
I iterate over every day which is between the both dates, get the day-name with date('l'); and check if it's within the array.
There may be some fine tuning need to be done, but this should get you going.
只是比“遍历所有日期”更快一点的方法:
这个想法是使用周一、周二、周三等的一些额外偏移量来计算周数。
Just a bit faster approach than "iterating through all days":
The idea is to count number of weeks using some additional offsets for mondays, tuesdays, wednesdays etc.