从 mysql 表中获取天数数组,其中包括 From/To 日期之间的天数

发布于 2024-10-31 05:34:40 字数 668 浏览 1 评论 0原文

如果这有点简单,我很抱歉,但我一直在谷歌搜索,但似乎找不到我要找的东西。

我有一个日历脚本,允许用户上传持续超过一天的事件。它将事件存储在 mysql 数据库表中,该表将起始日期和截止日期存储在单独的列中。

当我尝试从表中提取日期并将日期存储为数组(包括起始日期/截止日期之间的日期)以便可以在日历中突出显示它们时,脚本超时,告诉我它的内存不足起始日期/结束日期之间的日期行。当然,这是不对的,因为桌子只容纳我的测试活动,而且只持续一周。

可能有一种更简单的方法可以一起完成所有工作,如果我的脚本存在其他问题,我一点也不会感到惊讶,因此非常欢迎任何和所有输入。谢谢!

这是片段:

$FromDate=date("Y-m-d", strtotime($Calendar['FromDate']));
$ToDate=date("Y-m-d", strtotime($Calendar['ToDate']));
while($FromDate < $ToDate){
    // below is the line it times out on.
    $StartDate=date("Y-m-d", strtotime("+1 day", strtotime($FromDate))); 
    $EventDays[]=date("d", strtotime($StartDate));
}

I apologize if this is a little simple, but I've been googleing and googleing and just can't seem to find what I'm looking for.

I have a calendar script that allows users to upload events lasting more than one day. It stores the events in a mysql database table that stores the from date and to date in separate columns.

When I attempt to pull the dates from the table and store the days as an array, including those between the from/to dates, so that they can be highlighted in the calendar the script times out telling me that it ran out of memory on the line where dates between the from/to dates. Surely that can't be right as the table only holds my test event and it only lasts one week.

There may be an easier way of doing it all together and I wouldn't be at all surprised if there were other problems with my script, so any and all input is very much welcome. Thanks!

Here's the snipet:

$FromDate=date("Y-m-d", strtotime($Calendar['FromDate']));
$ToDate=date("Y-m-d", strtotime($Calendar['ToDate']));
while($FromDate < $ToDate){
    // below is the line it times out on.
    $StartDate=date("Y-m-d", strtotime("+1 day", strtotime($FromDate))); 
    $EventDays[]=date("d", strtotime($StartDate));
}

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

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

发布评论

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

评论(3

╰つ倒转 2024-11-07 05:34:40

它看起来像一个简单的无限循环,因为您从不更改 $FromDate$ToDate 因此 while(...) 永远不会停止并且您运行内存不足,因为您总是一次又一次地存储相同的日期。

我认为您想将 $StartDate 重命名为 $FromDate 以获得您谴责的行为。

It looks like a simple endless loop since you never change $FromDate or $ToDate so the while(...) never stops and you run out of memory since you always store the same date again and again.

I think you want to rename $StartDate to $FromDate to get the behaviour you decried.

旧人九事 2024-11-07 05:34:40

如果您使用的是 PHP 5.3,那么这是使用 DatePeriod 的绝佳机会。

<?php
$start = new DateTime('2011-01-01');
$interval = new DateInterval('P1D');
$end = new DateTime('2011-01-28');
// Modify end by one day, so the end date is included.
$period = new DatePeriod($start, $interval, $end->modify('+1 day'));
$eventdays = array();

foreach($period as $date) {
    $eventdays[] = $date->format('d');
} 

var_dump($eventdays);

If you are using PHP 5.3 then this is a great opportunity to use DatePeriod.

<?php
$start = new DateTime('2011-01-01');
$interval = new DateInterval('P1D');
$end = new DateTime('2011-01-28');
// Modify end by one day, so the end date is included.
$period = new DatePeriod($start, $interval, $end->modify('+1 day'));
$eventdays = array();

foreach($period as $date) {
    $eventdays[] = $date->format('d');
} 

var_dump($eventdays);
帥小哥 2024-11-07 05:34:40

为什么不

SELECT * FROM events WHERE datum BETWEEN '2011-4-6' AND '2011-4-12';

使用 SQL 然后使用 PHP 做其他事情呢?

Why don't you...

SELECT * FROM events WHERE datum BETWEEN '2011-4-6' AND '2011-4-12';

with SQL and then do other stuff with PHP?

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