代码之家  ›  专栏  ›  技术社区  ›  Urs

如何在PHP中从日期中获取仅时间的时间戳

  •  0
  • Urs  · 技术社区  · 6 年前

    我在PHP中有一个时间字段,它接收小时和分钟(H:I)。 \Datetime Object . 通过魔法,在我的情况下,不希望的是,当前日期被添加到那个时间。

     $timeonly = $this->getDate()->format('H:i');
    

    这给了我10点的时间。

    但现在。。。如何将此字符串转换为所需的整数 36000

    我试过了 strtotime($timeonly)

    这是不是一个好主意,难道我不觉得这是因为它太琐碎了,还是因为它没有完成?

    DateTime-> 喜欢 returnTimeWithoutDate ?

    3 回复  |  直到 6 年前
        1
  •  1
  •   Sean Bright Sean Stinehour    6 年前

    这只是一点点数学上的小事:

    function timeToSeconds(\DateTime $dt)
    {
        return $dt->format('H') * 3600 + $dt->format('i') * 60;
    }
    
    echo timeToSeconds($this->getDate());
    
        2
  •  1
  •   Abderrahim Soubai-Elidrisi    6 年前

    破解密码:

    $timeonly = $this->getDate()->format('H')*3600 + $this->getDate()->format('i')*60;
    
        3
  •  0
  •   Guillaume    6 年前

    $timeonly = $this->getDate()->format('H:i');
    $date = strtotime($timeonly);
    echo date('H', $date);