【发布时间】:2016-07-10 01:29:25
【问题描述】:
此代码是向FullCalendar 提供事件的函数的一部分,并在this 基础上稍作修改。我的编码知识充其量是最少的,现在已经学习了几天了。
- 我的 MYSQL 数据分为两部分
- 单独的日期和时间
- 当前函数只接受 DATETIME 或 TIMESTAMP 格式
解决方案:
- 将数组中的 DATE 和 TIME 组合成 DATETIME
让我们假设 MYSQL 表:
CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(11) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`sdate` date NOT NULL,
`stime` varchar(5) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`edate` date NOT NULL,
`etime` varchar(5) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
假设函数:
{
$events = array();
$query = mysqli_query($con, "SELECT * FROM test");
while($fetch = mysqli_fetch_array($query,MYSQLI_ASSOC))
{
$e = array();
$e['id'] = $fetch['id'];
$e['title'] = $fetch['title'];
$e['start'] = array_push($e,$fetch['sdate'],$fetch['stime']);
$e['end'] = array_push($e,$fetch['edate'],$fetch['etime']);
array_push($events, $e);
}
echo json_encode($events);
}
问题:
$e['start'] = array_push($e,$fetch['sdate'],$fetch['stime']);
$e['end'] = array_push($e,$fetch['edate'],$fetch['etime']);
无论输入什么数据,结果都是“4”。添加的数组越多,即使数据是字符串,$e 的值也只会增加 1。对我来说这是一个进步,因为我尝试过其他方法,如 $a + $b 或 array_combine,设置多个数组实例,如:
$e['start'] = $fetch['sdate'];
$e['start'] = $fetch['stime'];
$e['end'] = $fetch['edate'];
$e['end'] = $fetch['etime'];
而且没有一个有效。甚至可以组合数组以产生除其中包含的数组数量之外的其他内容吗?非常感谢大家的时间和阅读。
【问题讨论】:
-
您可以将两者(日期和时间)连接起来,并使用PHP中的
date()函数使其成为有效的日期格式:$e['start'] = date("Y-m-d H:i:s", strtotime($fetch['sdate'].' '.$fetch['stime'])); -
您的解决方案完全正确!
标签: php jquery mysql arrays ajax