【问题标题】:Convert army time to regular time AM PM in PHP [closed]在 PHP 中将军队时间转换为常规时间 AM PM [关闭]
【发布时间】:2013-06-06 16:47:22
【问题描述】:

我正在寻找一种使用 AM/PM 表达式将军队时间(例如 23:00)转换为常规时间的方法。

【问题讨论】:

  • 我认为“军队时间”更普遍地称为“24 hour clock:-p
  • $time24 = '23:00'; $dt = new DateTime($time24); echo $dt->format('h:i A');
  • @marabutt - 我认为你必须至少有 1K 的代表才能被允许在海报上发誓。
  • 从循环中试试这个:echo date("g:i a", strtotime($result['hourOpeningHour']))

标签: php time


【解决方案1】:

输入 $time 的格式为“XXXX”(例如“0000”、“0001”、...、“2300”)。我找不到为此的功能,所以写了以下内容。想在这里发帖,以防其他人也在寻找同样的东西。

function convert_army_to_regular($time) {
    $hours = substr($time, 0, 2);
    $minutes = substr($time, 2, 2);

    if ($hours > 12) { 
        $hours = $hours - 12;
        $ampm = 'PM';
    } else {
        if ($hours != 11) {
            $hours = substr($hours, 1, 1);
        }
        $ampm = 'AM';
    }
    return $hours . ':' . $minutes . $ampm;
}

【讨论】:

  • 最好将时间转换为 unix 时间戳,然后使用date 进行渲染——这非常灵活。不过,+1。
  • 显而易见的答案是将其转换为 unix 时间戳或 DateTime 对象,然后使用 date() 或 DateTime format() 方法......这样更简洁更容易
【解决方案2】:

http://www.php.net/manual/en/function.date.php

$army_time_str = "23:00";
$regular_time_str = date( 'g:i A', strtotime( $army_time_str ) );
echo $regular_time_str;

【讨论】:

  • 很好用..
【解决方案3】:

只需像这样在strtotime 函数中消磨时间:

$time_in_12_hour_format = date("g:i a", strtotime("23:00"));
echo $time_in_12_hour_format;

【讨论】:

  • 这是你要找的我的朋友。
猜你喜欢
  • 2022-01-23
  • 2011-12-29
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
相关资源
最近更新 更多