gpt4 book ai didi

php - fatal error :调用未定义的方法 DateTime::setTimestamp()

转载 作者:行者123 更新时间:2023-12-02 03:00:45 26 4
gpt4 key购买 nike

仅在一台特定服务器上出现上述错误,但在其他服务器上则没有。假设是php版本问题。这是触发错误的代码:

//get time interval
static function get_interval($now, $post_time){
$datetime1 = new DateTime();
$datetime1->setTimestamp($now);

最后一行导致了问题。关于如何解决这个问题有什么想法吗?

最佳答案

如果您的 PHP 版本低于 5.3,那么您可以使用此类来使用函数“setTimestamp”和“getTimestamp”:

<?php

class MyDateTime extends DateTime
{
public function setTimestamp( $timestamp )
{
$date = getdate( ( int ) $timestamp );
$this->setDate( $date['year'] , $date['mon'] , $date['mday'] );
$this->setTime( $date['hours'] , $date['minutes'] , $date['seconds'] );
}

public function getTimestamp()
{
return $this->format( 'U' );
}
}

$date = new MyDateTime();
$date->setTimestamp( $someTimestamp );

echo $date->format( 'd/m/Y H:i:s' );

?>

关于php - fatal error :调用未定义的方法 DateTime::setTimestamp(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16554714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com