如何在PHP中从日期获取仅限时间的时间戳
我在PHP中有一个接收小时和分钟(H:I)的时间字段。 这进入一个如何在PHP中从日期获取仅限时间的时间戳,php,datetime,Php,Datetime,我在PHP中有一个接收小时和分钟(H:I)的时间字段。 这进入一个\Datetime对象。 神奇的是,在我的例子中,不希望的是,当前日期被添加到该时间中 我想去掉日期,只保留时间。 $timeonly = $this->getDate()->format('H:i'); 这给了我10点的时间 但是现在。。。如何将此字符串转换为所需的整数36000,即10h 我尝试了strotime($timeonly)但这又添加了日期 这是一个好主意吗?我不觉得这是因为它很琐碎,还是因为它没有完
\Datetime对象
。
神奇的是,在我的例子中,不希望的是,当前日期被添加到该时间中
我想去掉日期,只保留时间。
$timeonly = $this->getDate()->format('H:i');
这给了我10点的时间
但是现在。。。如何将此字符串转换为所需的整数36000
,即10h
我尝试了strotime($timeonly)
但这又添加了日期
这是一个好主意吗?我不觉得这是因为它很琐碎,还是因为它没有完成
也许在
DateTime->
中有一些东西,比如returnTimeWithoutDate
?这是一个简单的数学问题:
function timeToSeconds(\DateTime $dt)
{
return $dt->format('H') * 3600 + $dt->format('i') * 60;
}
echo timeToSeconds($this->getDate());
这是一个简单的数学问题:
function timeToSeconds(\DateTime $dt)
{
return $dt->format('H') * 3600 + $dt->format('i') * 60;
}
echo timeToSeconds($this->getDate());
破解代码:
$timeonly = $this->getDate()->format('H')*3600 + $this->getDate()->format('i')*60;
破解代码:
$timeonly = $this->getDate()->format('H')*3600 + $this->getDate()->format('i')*60;
你应该试试这样的东西:
$timeonly = $this->getDate()->format('H:i');
$date = strtotime($timeonly);
echo date('H', $date);
你应该试试这样的东西:
$timeonly = $this->getDate()->format('H:i');
$date = strtotime($timeonly);
echo date('H', $date);
-潜在的重复,因为从技术上讲,你只对从给定的一天午夜起经过的秒数感兴趣(如果我理解正确的话)。PS我甚至尝试了打印(strotime($date->setDate(1970,1,0)->format('Y-m-dh:I:s');(试图将日期重置为0…可能不值得任何东西)我不习惯这么快的回答。。谢谢潜在的重复,因为从技术上讲,你只对从给定的一天午夜起经过的秒数感兴趣(如果我理解正确的话)。PS我甚至尝试了打印(strotime($date->setDate(1970,1,0)->format('Y-m-dh:I:s');(试图将日期重置为0…可能不值得任何东西)我不习惯这么快的回答。。谢谢