以 PHP5 中的数组形式返回当前日历周的日期

发布于 2024-08-23 08:12:20 字数 489 浏览 4 评论 0原文

我如何组合一个 PHP5 函数来查找当前日历周并以数组形式返回一周中每一天的日期(从星期一开始)?例如,如果该函数今天运行(2010 年 2 月 25 日星期四),该函数将返回一个数组,如下所示:

[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 />

日期以什么格式存储在数组中并不重要,因为我认为这很容易改变。另外,如果能够选择提供日期作为参数并获取该日期而不是当前日期的日历周,那就太好了。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小矜持 2024-08-30 08:12:20

我想解决方案是首先使用 strtotime< 获取与上周一相对应的时间戳/a>:

$timestampFirstDay = strtotime('last monday');

但是,如果您尝试今天(星期四),使用类似以下内容:

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

您将得到:

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

即上周...对于 strtotime,“last” 表示 “今天之前的那个”

这意味着您必须测试今天是否是 strtotime 返回的“上周一”加上一周 - 如果是,则添加一周...

这里是一个可能的解决方案(可能有更聪明的想法):

$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;
}

现在我们有了“上周一”的时间戳,我们可以编写一个简单的 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

现在,由您决定:

  • 修改 for 循环,以便将日期存储在数组中
  • 决定要为 date 函数

玩得开心;-)

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 ;-)

折戟 2024-08-30 08:12:20

承租人。 。首先,为了避免任何时区问题,我会将您想要比较的那一天的中午固定下来,因此无论哪种方式一个小时都不会造成任何问题。

$dateCompare = time(); // replace with a parameter
$dateCompare = mktime(12, 0, 0, date('n', $dateCompare), 
      date('j', $dateCompare), date('Y', $dateCompare));

然后找到您约会的星期几。

$dow = date('N', $dateCompare);

1 是星期一,所以算出距离星期一还有多少天。

$days = $dow - 1;

现在减去天数秒数,直到回到星期一。

$dateCompare -= (3600 * 24 * $days);

现在整理一下你的日子。

$output = array();
for ($x = 0; $x < 7; $x++) {
    $output[$x] = $dateCompare + ($x * 24 * 3600);
}

这是一个时间戳数组,但如果您愿意,可以将日期存储为字符串。

Lessee. . First, to avoid any timezone issues I'd peg to noon on the day you want to compare against, so an hour either way won't cause any problems.

$dateCompare = time(); // replace with a parameter
$dateCompare = mktime(12, 0, 0, date('n', $dateCompare), 
      date('j', $dateCompare), date('Y', $dateCompare));

Then find the day of week of your date.

$dow = date('N', $dateCompare);

1 is Monday, so figure out how many days from Monday we are.

$days = $dow - 1;

Now subtract days worth of seconds until you get back to Monday.

$dateCompare -= (3600 * 24 * $days);

Now assemble your array of days.

$output = array();
for ($x = 0; $x < 7; $x++) {
    $output[$x] = $dateCompare + ($x * 24 * 3600);
}

This is an array of timestamps, but you could store the dates as strings if you prefer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文