简体   繁体   English

在PHP范围内转换12到24小时的时间

[英]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 am5 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.

相关问题 在PHP中将24小时格式小时转换为12小时格式时间 - Convert 24 hour format hours into 12 hour format time in php 如何在 PHP 中将 12 小时制时间转换为 24 小时制时间? - How do I convert 12 hour clock time into 24 hour clock time in PHP? 为什么用PHP无法将我的24小时转换为上午12点/下午12点? - Why can't I convert my 24 hour time to a 12 hour am/pm time with PHP? 在PHP中如何在12小时和24小时之间转换? - How do you convert between 12 hour time and 24 hour time in PHP? 将12小时制转换为24小时制PHP - Convert 12hour clock to 24hour clock PHP 使用php / Mysql中的选择查询将日期/时间从24小时格式转换为12小时AM / PM - convert date/time from 24-hour format to 12-hour AM/PM using select query in php/Mysql 从PHP中没有AM和PM的12小时中减去24小时 - Deducing 24 hour time from a 12 hour time without AM and PM in PHP 将mySQL数据库的日志文件日期时间格式从12小时时钟批量转换为24小时时钟 - Bulk convert log file date time format from 12 hour clock to 24 hour clock for mySQL database 如何将日期/时间从24小时格式转换为12小时AM / PM? - How do I convert date/time from 24-hour format to 12-hour AM/PM? 如何确定当前语言环境在PHP中是否具有24小时或12小时时间格式? - How to determine if current locale has 24-hour or 12-hour time format in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM