以PHP5中的数组形式返回当前日历周的日期 [英] Return dates of current calendar week as array in PHP5

查看:140
本文介绍了以PHP5中的数组形式返回当前日历周的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何组合一个PHP5函数,它将找到当前日历周,并将星期中每一天的日期作为数组返回,从星期一开始?例如,如果函数今天运行(2010年2月25日),函数将返回一个数组,如:

  [0 ] => 2010年2月22日星期一< br /> 
[1] => Tue Feb 23 2010< br />
[2] => Wed Feb 24 2010< br />
[3] => Tu Feb 25 2010< br />
[4] => Fri Feb 26 2010< br />
[5] => Sat Feb 27 2010< br />
[6] => Sun 2010年2月28日< br />

不管什么格式的日期都存储在数组中, d很容易改变。此外,您可以选择提供日期作为参数,并获取该日期的日历周而不是当前日期。



谢谢! / p>

解决方案

我想假设一个解决方案将开始获取对应于最后一个星期一的时间戳,使用 strtotime

  $ timestampFirstDay = strtotime('last monday'); 



但如果您今天试用,具有这样的:

  $ timestampFirstDay = strtotime('last thursday'); 
var_dump(date('Y-m-d',$ timestampFirstDay));

您将获得:

  string'2010-02-18'(length = 10)

即上一周...对于strtotime, 表示



您必须测试今天是否由 strtotime 加上一周加上一周所返回的上一个星期 - 如果是,则添加一周...



以下是可能的解决方案:

pre> $ timestampFirstDay = strtotime('last monday');
if(date('Ym-d',$ timestampFirstDay)== date('Ym-d',time() - 7 * 24 * 3600)){
//我们是那一天。 =>添加一周
$ timestampFirstDay + = 7 * 24 * 3600;
}



现在我们有时间戳记last monday,我们可以写一个简单的 for 循环,循环7次,每次加1天,如下:

  $ currentDay = $ timestampFirstDay; 
for($ i = 0; $ i <7; $ i ++){
echo date('Y-m-d',$ currentDay)。 '< br />';
$ currentDay + = 24 * 3600;
}

这将给我们这种输出:

  2010-02-22 
2010-02-23
2010-02-24
2010-02-25
2010-02-26
2010-02-27
2010-02-28



现在,由您决定:




  • 修改 /en/function.date.phprel =nofollow> date 功能



有乐趣; - )


How would I put together a PHP5 function that would find the current calendar week and return the dates of each day in the week as an array, starting on Monday? For example, if the function were run today (Thu Feb 25 2010), the function would return an array like:

[0] => Mon Feb 22 2010<br />
[1] => Tue Feb 23 2010<br />
[2] => Wed Feb 24 2010<br />
[3] => Thu Feb 25 2010<br />
[4] => Fri Feb 26 2010<br />
[5] => Sat Feb 27 2010<br />
[6] => Sun Feb 28 2010<br />

It doesn't matter what format the dates are stored as in the array, as I assume that'd be very easy to change. Also, it'd be nice to optionally be able to supply a date as a parameter and get the calendar week of that date instead of the current one.

Thanks!

解决方案

I suppose a solution would be to start by getting the timestamp that correspond to last monday, using strtotime :

$timestampFirstDay = strtotime('last monday');


But if you try with today (thursday), with something like this :

$timestampFirstDay = strtotime('last thursday');
var_dump(date('Y-m-d', $timestampFirstDay));

you'll get :

string '2010-02-18' (length=10)

i.e. last week... For strtotime, "last" means "the one before today".

Which mean you'll have to test if today is "last monday" as returned by strtotime plus one week -- and, if so, add one week...

Here's a possible (there are probably smarter ideas) solution :

$timestampFirstDay = strtotime('last monday');
if (date('Y-m-d', $timestampFirstDay) == date('Y-m-d', time() - 7*24*3600)) {
    // we are that day... => add one week
    $timestampFirstDay += 7 * 24 * 3600;
}


And now that we have the timestamp of "last monday", we can write a simple for loop that loops 7 times, adding 1 day each time, like this :

$currentDay = $timestampFirstDay;
for ($i = 0 ; $i < 7 ; $i++) {
    echo date('Y-m-d', $currentDay) . '<br />';
    $currentDay += 24 * 3600;
}

Which will give us this kind of output :

2010-02-22
2010-02-23
2010-02-24
2010-02-25
2010-02-26
2010-02-27
2010-02-28


Now, up to you to :

  • Modify that for loop so it stores the dates in an array
  • Decide which format you want to use for the date function

Have fun ;-)

这篇关于以PHP5中的数组形式返回当前日历周的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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