[英]Convert 12 to 24 hour time within a range in PHP
I'm getting a time range like this from a field in WordPress: 我从WordPress的一个字段中得到这样的时间范围:
8 am - 5 pm
More than anything I just want to make sure that I'm thinking about the correctly. 最重要的是我只想确保我正确地思考问题。
I'm assuming that if I want to convert that time to 24 hour format, I'll need to strip out the dash -
first and then convert each hour 8 am
and 5 pm
to be individual time numbers. 我假设如果我想将该时间转换为24小时格式,我需要先删除短划线-
然后将每小时的8 am
和5 pm
转换为单独的时间数字。 So something like this?: 这样的事情呢?:
$newHours = explode('-',$hours);
$time_am = date("H:i", strtotime($newHours[0]));
$time_pm = date("H:i", strtotime($newHours[1]));
Wondering if there is a possible better way of going about that especially if I'm getting hours for multiple days as well. 想知道是否有更好的方法可以解决这个问题,特别是如果我也需要多天的工作时间。
You can try something like this: 你可以尝试这样的事情:
// 24-hour time to 12-hour time // 24小时到12小时的时间
$time_in_12_hour_format = date("g:ia", strtotime("13:30")); $ time_in_12_hour_format = date(“g:ia”,strtotime(“13:30”));
// 12-hour time to 24-hour time // 12小时到24小时的时间
$time_in_24_hour_format = date("H:i", strtotime("1:30 PM")); $ time_in_24_hour_format = date(“H:i”,strtotime(“1:30 PM”));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.