John Davidson

php - Summarise open hours array to natural language string

0 comments
Message:


I have a list of open and close timings saved in an array like this


{
"monday": {
"working": "yes",
"open": "09:00",
"close": "17:00"
},
"tuesday": {
"working": "yes",
"open": "09:00",
"close": "17:00"
},
"wednesday": {
"working": "yes",
"open": "09:00",
"close": "17:00"
},
"thursday": {
"working": "yes",
"open": "09:00",
"close": "18:00"
},
"friday": {
"working": "yes",
"open": "09:00",
"close": "17:00"
},
"saturday": {
"working": "yes",
"open": "09:00",
"close": "14:00"
},
"sunday": {
"open": "00:00",
"close": "00:00"
}
}


I need to create a string that would most naturally describe the timings in one line.
So for the above the expected output is


$result = 'mon to wed and fri: 09:00 to 17:00, thu: 09:00 to 18:00, sat: 09:00 to 14:00';

if using and is difficult I can also use a , (comma) like this


$result = 'mon to wed, fri: 09:00 to 17:00, thu: 09:00 to 18:00, sat: 09:00 to 14:00';

The array is created to account for different conditions so a valid timing could also be
mon to fri: 09:00 to 17:00


I am confused how to get this done. I can iterate over the array and manually build something like that but is that a good approach.


As of now I have done something like this


$openHoursReduced = [];
$openHoursJson = '{"monday":{"working":"yes","open":"09:00","close":"17:00"},"tuesday":{"working":"yes","open":"09:00","close":"17:00"},"wednesday":{"working":"yes","open":"09:00","close":"17:00"},"thursday":{"working":"yes","open":"09:00","close":"18:00"},"friday":{"working":"yes","open":"09:00","close":"17:00"},"saturday":{"working":"yes","open":"09:00","close":"14:00"},"sunday":{"open":"00:00","close":"00:00"}}';
$openHoursArray = json_decode($openHoursJson, true);
foreach ($openHoursArray as $dayOfWeek => $dayConfigs) {
if(!empty($dayConfigs['working']) && $dayConfigs['working'] == 'yes'){
$hoursStr = sprintf('%s to %s', $dayConfigs['open'], $dayConfigs['close']);
$openHoursReduced[$hoursStr][] = $dayOfWeek;
}

}
var_dump($openHoursReduced);

And the results I get are


array (size=3)
'09:00 to 17:00' =>
array (size=4)
0 => string 'monday' (length=6)
1 => string 'tuesday' (length=7)
2 => string 'wednesday' (length=9)
3 => string 'friday' (length=6)
'09:00 to 18:00' =>
array (size=1)
0 => string 'thursday' (length=8)
'09:00 to 14:00' =>
array (size=1)
0 => string 'saturday' (length=8)

Unsure how to go about creating the text representation.


What is the best approach here. Any suggestion is very helpful.


Thanks in advance.



My Answer:
One approach to create the text representation is to iterate over the reduced array and build the string accordingly. Here's an example of how you can achieve this:

php
$openHoursString = '';
foreach ($openHoursReduced as $hours => $days) {
$daysString = implode(', ', $days);
$openHoursString .= $daysString . ': ' . $hours . ', ';
}

// Remove the trailing comma and space
$openHoursString = rtrim($openHoursString, ', ');

echo $openHoursString;


This code will iterate over the reduced array and build the string in the format you specified. The `implode` function is used to concatenate the days of the week, and the resulting string is then concatenated with the open and close hours for each day. Finally, the trailing comma and space are removed from the final string.

You can then use the `$openHoursString` variable to display the open hours in the desired format.

Rate this post

5 of 5 based on 8420 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us