自定义 codeigniter 库中未定义的偏移量,我做错了什么?

Undefined offset in custom codeigniter library, what am i doing wrong?

我的代码中有一个视图 php 错误,这会导致 $time 和 $date 数组出现两个错误。 Php 说消息:未定义偏移量:$time 和 $date 均为 1 和 2。

所以我完成 explode() 后的键未定义?这是怎么回事,为什么会这样?我该如何解决这个问题?数组显示结果来自数据库,我在使用 print_r 时可以看到。

 Array ( [0] => 05 [1] => 51 [2] => 00 ) 
 Array ( [0] => 1984 [1] => 06 [2] => 23 )

我尝试了很多东西,none 似乎有效。我不记得我以前遇到过这个问题......我几乎会开始思考这个问题可能与 Codeigniter 有关吗?

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sweph {
 
 public $julian_ut = null; 
 
 public $config = array();
 
 public $chart_id='';
 
 public $user_id='';
 
 public $chart_name='';
 
 public $chart_time='';
 
 public $chart_date='';
 
 public $chart_search='';
 
 public $chart_timezone='';
 
 public $chart_houses='';
 
 public $chart_longitude='';
 
 public $chart_latitude='';
 
 public $time = array();
 
 public $date = array();
 
 /***
  * Initialize chart details
  **/
 public function __construct($config=array()) {

  // Get instance 
  $this->CI =& get_instance();  
  
  // Initializes and Loads Ephemeris files from Directory
  swe_set_ephe_path(FCPATH.'ephemeris');
 
  // Creates objects from config array
  foreach ($config as $property => $value)
                {
                     $this->$property = $value;
                } 
  
  // Create time and date array
  $time = explode(":", $this->chart_time);
  $date = explode("-", $this->chart_date);
  
  // Calculates Julian day 
  $this->julian_ut = swe_julday(intval($date[0]), $date[1], $date[2], ($time[0] + $time[1] / 60 + $time[2] / 3600) , SE_GREG_CAL); 
  
        }

 /***
  * Return time/day in julian day number
  **/ 
 public function get_julian_ut() {
  return $this->julian_ut;
 }

}

** 错误 **

遇到了一个PHP错误

严重性:通知

消息:未定义的偏移量:1

文件名:libraries/Sweph.php

行号:61

遇到了一个PHP错误

严重性:通知

消息:未定义的偏移量:2

文件名:libraries/Sweph.php

行号:61

遇到了一个PHP错误

严重性:通知

消息:未定义的偏移量:1

文件名:libraries/Sweph.php

行号:61

遇到了一个PHP错误

严重性:通知

消息:未定义的偏移量:2

文件名:libraries/Sweph.php

行号:61

更新: 忘记提及数组有来自数据库的结果并添加了错误。

 $time = explode(":", $this->chart_time);
 //it seems $time does not have value for 1,2 index 
 //$time[1] $time[2] does not exists

这就是你收到警告的原因。

相同
 $date = explode("-", $this->chart_date);

确保 $time 最小为 3 size.It 如果大小小于 3 将产生相同的警告。

检查它的var_dump()然后相应地使用索引

$time = explode(":", $this->chart_time);
var_dump($time);

$date = explode("-", $this->chart_date);
var_dump($date);

但这不是获取日期、月份或时间的正确方法


推荐:

<?php
$date = '2015-01-22';
$time = '18:16:12';

$dt = new DateTime($date);
$tm = new DateTime($time);

$day = $dt->format('d');
$month = $dt->format('m');
$year = $dt->format('Y');

$hour = $tm->format('H');
$min = $tm->format('i');
$sec = $tm->format('s');


演示:
http://3v4l.org/VDB7l


文档:
http://docs.php.net/class.datetime