I'm trying to convert the following XML into a Time:
我想把下面的XML转换成时间:
<time hour="18" minute="05" timezone="Eastern" utc-hour="-4" utc-minute="00" />
I'm parsing the XML using SimpleXML, and storing that in a class. I also tried printing out the following:
我使用SimpleXML解析XML,并将其存储在类中。我也尝试打印以下内容:
print strtotime($x->time->attributes()->hour . ":" . $x->time->attributes()->minute) . "<br>\n";
The results I got from that print looked something like this:
我从打印出来的结果是这样的:
1249254300
I guess the result I'm expecting would be either 18:05 or 6:05 PM
我猜我预期的结果是18:05或者6:05
Is there any way I can get it to convert to the style I'm expecting? Also, if I were to add the timezone onto the end is there any way for it to figure that out?
我有什么办法把它转换成我想要的样式吗?另外,如果我把时区加到最后有没有办法让它算出来?
3 个解决方案
#1
4
I would advise against using strtotime here. Instead try mktime.
我建议不要在这里使用strtotime。而不是尝试mktime。
$time=mktime($x->time->attributes()->hour,$x->time->attributes()->minute,0,0,0,0)
Also you need to format the time stamp (what you printed out was the unix representation). You can do this with the date function.
您还需要格式化时间戳(您输出的是unix表示)。您可以使用date函数来实现这一点。
date("H:i", $time);
#2
1
You can bring it into a form strtotime() understands (including the timezone info)
您可以将它转换成strtotime()理解的表单(包括时区信息)
<?php
$x = new SimpleXMLElement('<x>
<time hour="18" minute="05" timezone="Eastern" utc-hour="-4" utc-minute="00" />
</x>');
$ts = sprintf('%02d:%02d %+03d%02d', $x->time['hour'], $x->time['minute'], $x->time['utc-hour'], $x->time['utc-minute']);
$ts = strtotime($ts);
echo gmdate(DateTime::RFC1123, $ts), "\n", date(DateTime::RFC1123, $ts);
My local timezone is CET/CEST (utc+2 in this case), therefore the script prints
我的本地时区是CET/CEST(在本例中是utc+2),因此脚本打印。
Mon, 03 Aug 2009 22:05:00 +0000
Tue, 04 Aug 2009 00:05:00 +0200
#3
1
The strToTime
function will recognize a date or time in string format, and then convert it to a Unix Timestamp (the number of seconds since the Unix Epoch (the Unix epoch being January 1, 1970 a midnight, UTC).
strToTime函数将以字符串格式识别日期或时间,然后将其转换为Unix时间戳(Unix纪元后的秒数(Unix纪元是1970年1月1日午夜UTC)。
So, what you need to do is take the resul of your strToTime function, and use the
date
function to format them in a more human readable way
因此,您需要做的是获取strToTime函数的结果,并使用date函数以更人性化的方式格式化它们
//24 hour clock
print date(
'H:i',
strtotime($x->time->attributes()->hour . ":" . $x->time->attributes()->minute) . "<br>\n");
//12 hour clock
print date(
'h:i a',
strtotime($x->time->attributes()->hour . ":" . $x->time->attributes()->minute) . "<br>\n");
Full formatting options can be found the date entry on php.net
可以在php.net上找到完整的格式化选项。
#1
4
I would advise against using strtotime here. Instead try mktime.
我建议不要在这里使用strtotime。而不是尝试mktime。
$time=mktime($x->time->attributes()->hour,$x->time->attributes()->minute,0,0,0,0)
Also you need to format the time stamp (what you printed out was the unix representation). You can do this with the date function.
您还需要格式化时间戳(您输出的是unix表示)。您可以使用date函数来实现这一点。
date("H:i", $time);
#2
1
You can bring it into a form strtotime() understands (including the timezone info)
您可以将它转换成strtotime()理解的表单(包括时区信息)
<?php
$x = new SimpleXMLElement('<x>
<time hour="18" minute="05" timezone="Eastern" utc-hour="-4" utc-minute="00" />
</x>');
$ts = sprintf('%02d:%02d %+03d%02d', $x->time['hour'], $x->time['minute'], $x->time['utc-hour'], $x->time['utc-minute']);
$ts = strtotime($ts);
echo gmdate(DateTime::RFC1123, $ts), "\n", date(DateTime::RFC1123, $ts);
My local timezone is CET/CEST (utc+2 in this case), therefore the script prints
我的本地时区是CET/CEST(在本例中是utc+2),因此脚本打印。
Mon, 03 Aug 2009 22:05:00 +0000
Tue, 04 Aug 2009 00:05:00 +0200
#3
1
The strToTime
function will recognize a date or time in string format, and then convert it to a Unix Timestamp (the number of seconds since the Unix Epoch (the Unix epoch being January 1, 1970 a midnight, UTC).
strToTime函数将以字符串格式识别日期或时间,然后将其转换为Unix时间戳(Unix纪元后的秒数(Unix纪元是1970年1月1日午夜UTC)。
So, what you need to do is take the resul of your strToTime function, and use the
date
function to format them in a more human readable way
因此,您需要做的是获取strToTime函数的结果,并使用date函数以更人性化的方式格式化它们
//24 hour clock
print date(
'H:i',
strtotime($x->time->attributes()->hour . ":" . $x->time->attributes()->minute) . "<br>\n");
//12 hour clock
print date(
'h:i a',
strtotime($x->time->attributes()->hour . ":" . $x->time->attributes()->minute) . "<br>\n");
Full formatting options can be found the date entry on php.net
可以在php.net上找到完整的格式化选项。