[英]How to get time only timestamp from a date in PHP
I have a time field in a PHP that receives Hours and minutes (H:i). 我在PHP中有一个时间字段,可以接收小时和分钟(H:i)。 This goes into a
\\Datetime Object
. 这进入
\\Datetime Object
。 By magic and, in my case, undesiredly, the current Date is added to that time. 通过魔术,在我的情况下,不希望有的是,将当前日期添加到该时间。
I would like to strip the date and keep the time only. 我想删除日期,只保留时间。
$timeonly = $this->getDate()->format('H:i');
This gives me the string 10:00. 这给了我字符串10:00。
But now ... how can this string be converted into the desired integer 36000
which is 10h? 但是现在……如何将这个字符串转换为所需的整数
36000
,即10h?
I tried strtotime($timeonly)
but this added the date again... 我尝试了
strtotime($timeonly)
但这又增加了日期...
Is this even a good idea, and do I not find it because it's so trivial or because this isn't done? 这是否是个好主意,是否因为它太琐碎或没有完成而找不到?
Maybe there's something in DateTime->
like returnTimeWithoutDate
? 也许
DateTime->
诸如returnTimeWithoutDate
类的returnTimeWithoutDate
?
This is trivial with a little bit of math: 借助一些数学知识,这是微不足道的:
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;
You should try sometings like this : 你应该尝试这样的东西:
$timeonly = $this->getDate()->format('H:i');
$date = strtotime($timeonly);
echo date('H', $date);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.