I have a set of arrays like this in PHP.
["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?
CodePudding user response:
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.
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 );
CodePudding user response:
PHP has easy to use date and string format functions.
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('d:00-d:00', date('H', strtotime($from)), date('H', strtotime($to)));
}
print_r($time24);
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
)