简体   繁体   English

如何将工作时间数组转换为 24 小时格式?

[英]How to convert array of working hours into 24 hour format?

I have a set of arrays like this in PHP.我在 PHP 中有一套像这样的 arrays。

  ["Monday"]=> string(7) "8AM-4PM" 
  ["Tuesday"]=> string(7) "8AM-4PM" 
  ["Wednesday"]=> string(7) "8AM-4PM" 
  ["Thursday"]=> string(7) "8AM-4PM" 
  ["Friday"]=> string(6) "Closed" 
  ["Saturday"]=> string(7) "8AM-4PM" 
  ["Sunday"]=> string(7) "8AM-4PM" }

How to I convert the hours into 24 hour format?如何将小时转换为 24 小时格式?

First of all, you'll have to loop through your array, then you'll have to check if the value has AM or PM because we don't want to transform Closed then you'll have to split the value by "-" then get an array of times and loop through it and then date and strtotime function you'll convert it to 24 hour time then you'll join the array with "-" again.首先,你必须遍历你的数组,然后你必须检查值是否有 AM 或 PM,因为我们不想转换Closed然后你必须用“-”分割值然后获取时间数组并循环遍历它,然后datestrtotime function 您将其转换为 24 小时时间,然后您将再次使用“-”加入数组。

Here is the working code:这是工作代码:

// Define data.
$data = array(
    'Monday'    => '8AM-4PM',
    'Tuesday'   => '8AM-4PM',
    'Wednesday' => '8AM-4PM',
    'Thursday'  => '8AM-4PM',
    'Friday'    => 'Closed',
    'Saturday'  => '8AM-4PM',
    'Sunday'    => '8AM-4PM',
);

// Creaye empty array to store transformed data.
$new_data = array();

// Loop through data.
foreach ( $data as $key => $value ) {
    // Check if the value has am or pm
    if ( strpos( strtolower( $value ), 'am' ) !== false || strpos( strtolower( $value ), 'pm' ) !== false ) {
        // split time from "-"
        $times = explode( '-', $value );
        // create an empty array to store transformed time.
        $new_times = array();
        // loop through times array.
        foreach ( $times as $time ) {
            // convert to 24 format.
            $new_times[] = date( 'HA', strtotime( $time ) );
        }
        // join the transformed times array with "-"
        // and store in new data.
        $new_data[ $key ] = join( '-', $new_times );
    } else {
        // if the value has no am or pm, just store it.
        $new_data[ $key ] = $value;
    }
}

var_dump( $new_data );

Here is the shorter version for the same thing, but it might be difficult for you to understand这是同一事物的较短版本,但您可能难以理解

// Define data.
$data = array(
    'Monday'    => '8AM-4PM',
    'Tuesday'   => '8AM-4PM',
    'Wednesday' => '8AM-4PM',
    'Thursday'  => '8AM-4PM',
    'Friday'    => 'Closed',
    'Saturday'  => '8AM-4PM',
    'Sunday'    => '8AM-4PM',
);

$data = array_map(
    function( $value ) {
        return strpos( strtolower( $value ), 'am' ) !== false || strpos( strtolower( $value ), 'pm' ) !== false ? join(
            '-',
            array_map(
                function( $time ) {
                    return date( 'HA', strtotime( $time ) );
                },
                explode( '-', $value )
            )
        ) : $value;
    },
    $data
);

var_dump( $data );

PHP has easy to use date and string format functions. PHP 具有易于使用的日期和字符串格式功能。

This loops over the array, and splits the time and reformats it.这循环遍历数组,分割时间并重新格式化它。

$times = [
    'Monday'    => '8AM-4PM',
    'Tuesday'   => '8AM-4PM',
    'Wednesday' => '8AM-4PM',
    'Thursday'  => '8AM-4PM',
    'Friday'    => 'Closed',
    'Saturday'  => '8AM-4PM',
    'Sunday'    => '8AM-4PM',
];

$time24 = [];
foreach ($times as $day => $time) {
    [$from, $to] = explode('-', $time);
    $time24[$day] = null === $to
        ? $from
        : sprintf('%02d:00-%02d:00', date('H', strtotime($from)), date('H', strtotime($to)));
}

print_r($time24);

Output Output

Array
(
    [Monday]    => 08:00-16:00
    [Tuesday]   => 08:00-16:00
    [Wednesday] => 08:00-16:00
    [Thursday]  => 08:00-16:00
    [Friday]    => Closed
    [Saturday]  => 08:00-16:00
    [Sunday]    => 08:00-16:00
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM