我正在使用 PHP/MySQL 创建一个事件系统,并使用 PHP 将日期拼凑在一起形成一个字符串。我希望日期格式为 dd-mm-yy。我正在使用以下代码:
$title = $_POST['title'];
$title = htmlspecialchars($title);
$type = $_POST['evtype'];
$type = htmlspecialchars($type);
$evdesc = $_POST['evdesc'];
$evdesc = htmlspecialchars($evdesc);
$startdate = ( isset( $_POST['startdate'] ) ) ? $_POST['startdate'] : '' ;
$starttime = $_POST['starttime'];
$starttime = htmlspecialchars($starttime);
$enddate = ( isset( $_POST['enddate'] ) ) ? $_POST['enddate'] : '' ;
$endtime = $_POST['endtime'];
$endtime = htmlspecialchars($endtime);
$location = $_POST['location'];
$location = htmlspecialchars($location);
// assemble dates and times
list($startday, $startmonth, $startyear) = array_pad(explode('-', $startdate, 2), 2, null);
$evstart = '' . $startyear . '-' . $startmonth . '-' . $startday . ' ' . $starttime . '';
list($endday, $endmonth, $endyear) = array_pad(explode('-', $enddate, 2), 2, null); // explode("-", $enddate);
$evend = '' . $endyear . '-' . $endmonth . '-' . $endday . ' ' . $endtime . '';
// end assemble
但是,此代码不会分解数据,稍后发布到数据库的唯一内容是“--(此处的时间)”
错误是:
注意:未定义偏移:calendar.php 第 42 行中的 2
注意: undefined offset :calendar.php 第 45 行中的 2
最佳答案
您正在使用
array_pad( /* something*/, 2, null)
并且期望在数组中获得三个项目。
list($startday, $startmonth, $startyear) = ...
我认为如果你将这个 2
更改为 3
,它会工作得很好。
(就此而言,您的 explode()
应该有 limit = 3
,而不是 2
)
关于PHP 未定义偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18034303/