从2个给定日期找到星期几多少? [英] find how many sunday from 2 given dates?

查看:124
本文介绍了从2个给定日期找到星期几多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从01-01-2009到02-23-2009

from 01-01-2009 to 02-23-2009

如何找出这几天之间有多少星期天?

how to find out how many Sunday in between these days??

推荐答案

这样的事情如何:

$date = strtotime('2009-01-01 next sunday');
$dateMax = strtotime('2009-02-23');

$nbr = 0;
while ($date < $dateMax) {
  var_dump(date('Y-m-d', $date));
  $nbr++;
  $date += 7 * 24 * 3600;
}

var_dump($nbr);

你得到的输出是:

string '2009-01-04' (length=10)
string '2009-01-11' (length=10)
string '2009-01-18' (length=10)
string '2009-01-25' (length=10)
string '2009-02-01' (length=10)
string '2009-02-08' (length=10)
string '2009-02-15' (length=10)
string '2009-02-22' (length=10)

对于每个星期日的日期,以及:

For the dates of each sunday, and :

int 8

星期日的数量

(我已经很快检查,似乎这些日期确实是星期日)



这段代码是:


What this code does is :


  • 首先,在2009-01-01之后获得第一个星期日的日期

  • 然后,只要当前日期在2009-02-23之前,每次迭代迭代

  • ,添加7 * 24 * 3600秒(每天7天周,每天24小时,每3600秒小时)

  • first, get the date of the first sunday after 2009-01-01
  • then, as long as the current date is before 2009-02-23, iterate
  • at each iteration, add 7*24*3600 seconds (7 days per week, 24 hours per day, 3600 seconds per hour)

注意:我已更改日期的格式,以使用YYYY-MM-DD,以及不是DD-MM-YYY;它是通常使用的格式 - 所以,使用这个格式可能是一个好主意,以确保strtotime正常工作。 (不确定您的格式可以正常工作)

Note : I've changed the format of your dates, to use YYYY-MM-DD, and not DD-MM-YYY ; it is the format that's generally used -- so, probably a good idea to use this one, to be sure strtotime works properly. (not sure it'll work OK with your format)

这篇关于从2个给定日期找到星期几多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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