加上1小时的时间戳


Plus 1 hour to timedatestamp

我有一个将长日期时间转换为人类可读格式的函数。然而,我需要在这些时间上增加一个小时。下面是我累了,但它不工作。我原来的函数在下面,它工作,但没有增加额外的1小时的时间。

不工作添加额外的小时功能

date_default_timezone_set('Europe/London');
    $date = date('Y-m-d');
    function convertDateTime($datestamp) {
   $dt = new DateTime("@$datestamp");
   $newdate= $dt->format('Y-m-d H:i:s');
   return $newdate("+1 hour");
}

输出不加小时的工作功能

date_default_timezone_set('Europe/London');
    $date = date('Y-m-d');
    function convertDateTime($datestamp) {
   $dt = new DateTime("@$datestamp");
   $newdate= $dt->format('Y-m-d H:i:s');
   return $newdate;

请尝试一下

    date_default_timezone_set('Europe/London');
    $date = date('Y-m-d');
    function convertDateTime($datestamp) {
    return  date("Y-m-d H:i:s", $datestamp+3600);
    }

您可以使用DateInterval并添加特定的小时数:

$date_ = new DateTime();
$date_->add(new DateInterval('P4H'));

别白费力气了。进入https://packagist.org.

搜索date .

一个很受欢迎的包似乎是:nestbot/carbon。

 composer require nesbot/carbon

使用它:

 $dt = new Carbon("@$datestamp");
 $dt->addHour(1);
 $formattedString = $dt->format('Y-m-d H:i:s');