PHP DateTime __construct() Failed to parse time string (xxxxxxxx) at position x

后端 未结 5 1931

I had this construction error when trying to creating a new DateTime object using a timestamp:

Exception: DateTime::_construct(): Failed to parse

5条回答
  •  感情败类
    2020-12-24 11:11

    This worked for me.

       /**
         * return date in specific format, given a timestamp.
         *
         * @param  timestamp  $datetime
         * @return string
         */
        public static function showDateString($timestamp)
        {
          if ($timestamp !== NULL) {
            $date = new DateTime();
            $date->setTimestamp(intval($timestamp));
            return $date->format("d-m-Y");
          }
          return '';
        }
    

提交回复
热议问题