PHP 未定义偏移量

标签 php mysql date explode

我正在使用 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)

<强> array_pad() reference

关于PHP 未定义偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18034303/

相关文章:

mysql - 查找给定 PK 的位置以及下一行和上一行作为一个结果行

PHP/MySQL 从选择中复制值

php - 显示今天的生日

bash - 如何找到两个日期之间的天数差异?

javascript - 设置日期变量的格式

php - 更改 Apache 配置文件后的 Symfony 路由问题

PHP 数组键存在于字符串中

php - 修改以使用 PHP 和 AJAX 在数据库中添加当前页面的 ID

php - 使用 Symfony 2.8 中新的 LDAP 组件进行身份验证

MySQL/phpMyadmin : "many connection errors"