从 mysql 表中获取天数数组,其中包括 From/To 日期之间的天数
如果这有点简单,我很抱歉,但我一直在谷歌搜索,但似乎找不到我要找的东西。
我有一个日历脚本,允许用户上传持续超过一天的事件。它将事件存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它看起来像一个简单的无限循环,因为您从不更改
$FromDate
或$ToDate
因此while(...)
永远不会停止并且您运行内存不足,因为您总是一次又一次地存储相同的日期。我认为您想将
$StartDate
重命名为$FromDate
以获得您谴责的行为。It looks like a simple endless loop since you never change
$FromDate
or$ToDate
so thewhile(...)
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.如果您使用的是 PHP 5.3,那么这是使用 DatePeriod 的绝佳机会。
If you are using PHP 5.3 then this is a great opportunity to use DatePeriod.
为什么不
使用 SQL 然后使用 PHP 做其他事情呢?
Why don't you...
with SQL and then do other stuff with PHP?