V
V
Vladimir Tyutimov2015-11-16 16:44:19
PHP
Vladimir Tyutimov, 2015-11-16 16:44:19

How to fix error in Call to a member function format() on a non-object?

Hello. After moving the site to another hosting, the following code does not work:

$Stroke = $Year.'-'.$Month.'-'.$Day.' '.$Hours.':'.$Minutes.':'.$Seconds;
        $format = 'Y-m-d H:i:s';
  $date = DateTime::createFromFormat($format, $Stroke);
  if ($Format == ''){
    $Format = 'd.m.Y H:i:s';
  }
  $result =  $date->format($Format);

the last line causes Fatal Error: Fatal error: Call to a member function format() on a non-object
How to fix the error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Puma Thailand, 2015-11-16
@opium

you have DateTime::createFromFormat($format, $Stroke); returns false
- check the contents of $Stroke
- check the time zone settings

A
Alexander Latyshev, 2015-11-16
@magalex

See what the DateTime::getLastErrors() function returns.
Alternatively, the time zone may not be set on the server - try using the full format of the DateTime::createFromFormat function :

$date = DateTime::createFromFormat($format, $Stroke, new DateTimeZone('Europe/Moscow'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question