I have this code...
class log{
private $dateTime;
private $message;
function log($dt, $message){
$this->dateTime = DateTime::createFromFormat('d/m/Y H:i:s', substr($dt, 0, -1));
$this->message = $message;
}
function get_dateTime(){
return $this->dateTime->format('d/m/Y H:i:s');
}
function get_message(){
return $this->message;
}
}
In parameter $dt
has format 17/05/2019 03:15:42:
. I get it by reading a text file.
I read the file and create the class object:
while (($line = fgets($handle)) !== false && $line !="\n") { //format example : 30/04/2019 07:59:14: File creation.
$date = strtok($line, " ");
$hour = strtok(" ");
$message = strtok("");
$log = new log($date." ".$hour,$message);
echo $log->get_dateTime()."<br>";
}
The result is: Fatal error: Call to a member function format() on a non-object in...
If I pass the parameter "manually" it works fine.
$log2 = new log('30/04/2019 07:59:14:',"test");
echo $log2->get_dateTime()."<br>";
If I use code
echo $date;
Correct date is returned. Same if I make echo $hour;
How is this possible?