解析格式化的日期时间表达式并将数字分配给变量
我从 eBay 网络服务获得了这个字符串。
// 2天23小时37分50秒
$字符串 = P2DT23H37M50S
我使用以下代码来提取时间和日期:
$daysLeft = substr($string, 1, 1) 。 “天”;
$hoursLeft = substr($string, 4, 2) 。 “小时”;
$minutesLeft = substr($string, 7, 2) 。 “分钟”;
$secondsLeft = substr($string, 10, 2) 。 “秒”;
但问题是,当小时、分钟和日期只有 1 位数字时,字符串就会变得混乱,我最初可以正常工作,但我有点坚持寻找一种简单的方法来做到这一点,我打算将其写在一堆 If 语句中,但确实想要一种更简单的方法来做到这一点。
I have this string from eBay Web-Service.
// 2Days 23Hours 37Minutes 50Seconds
$string = P2DT23H37M50S
I was using the following code to extract and time and date:
$daysLeft = substr($string, 1, 1) . " Days";
$hoursLeft = substr($string, 4, 2) . " Hours";
$minutesLeft = substr($string, 7, 2) . " Minutes";
$secondsLeft = substr($string, 10, 2) . " Seconds";
But the problem is, When the hours, minutes and dates are only 1 Digit, the strings gets all messed up, I had it working initially, but I'm kind of stuck to find a simple way to do it, I was going to write it in a bunch of If statements, but really want a more simple way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 sscanf (与 sprintf 相反)怎么样?
How about using sscanf (the opposite of sprintf)?
如果现在所有字段始终都在那里,只需将字符串 preg_split 为非数字即可:
If you now that all fields always is there, just preg_split the string on non-numbers: