I've asked a question here on combining days where the opening hours of a shop are similar
Combine days where opening hours are similar
Now I need some help in doing the opposite: Taking strings that contains the combined days and splitting them up into individual days, and store them in an array in order of the day (Mon first, Sun last).
For example, here is a set of strings that needs to be converted (exact same format):
Mon-Fri 11 am - 7:30 pm
Sat 11 am - 6 pm
Both strings need to be combined together and converted to this array:
array[0] = "11am - 7:30pm"
array[1] = "11am - 7:30pm"
array[2] = "11am - 7:30pm"
array[3] = "11am - 7:30pm"
array[4] = "11am - 7:30pm"
array[5] = "11am - 6pm"
array[6] = "" // Blank value if the day Sunday is not in the set of strings that needs conversion
// ideally it should be an associative array, ie. array['mon'] = "11am - 7:30pm"
Btw, I'm using Codeigniter PHP framework and jQuery.
UPDATE:
I now have to split this string of text containing commas!
Mon 9 pm - 1 am
Tue-Wed, Sun 7:30 pm - 1 am
Thu-Sat 7:30 pm - 2 am
Resulting array:
Array
(
[Mon] => 9 pm - 1 am
[Tue] =>
[Wed] =>
[Thu] => 7:30 pm - 2 am
[Fri] => 7:30 pm - 2 am
[Sat] => 7:30 pm - 2 am
[Sun] =>
)
Looks like I have to split Tue-Wed from Sun, from
Tue-Wed, Sun 7:30 pm - 1 am
to
Tue-Wed 7:30 pm - 1 am
Sun 7:30 pm - 1 am
@Khattam: This chunk of timings:
Mon 9 pm - 1 am
Tue-Wed, Sun 7:30 pm - 1 am
Thu-Sat 7:30 pm - 2 am
gives:
Array
(
[Mon] => 9 pm - 1 am
[Tue] => 7:30 pm - 1 am
[Wed] => 7:30 pm - 1 am
[Thu] => 7:30 pm - 2 am
[Fri] => 7:30 pm - 2 am
[Sat] => 7:30 pm - 2 am
[Sun] =>
[
] =>
)
You could also use a controller action with the code similar to following:
<?php
$days = array(
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
'Sun'
);
$combined = "Tue-Wed, Sun 7:30 pm - 1 am";
$combined = explode("\n",$combined);
foreach($combined as &$c){
$commaPosition = strpos($c,', ');
if($commaPosition!==false){
array_push($combined,substr($c,$commaPosition+2));
$spacePosition = strpos($c,' ',$commaPosition+2);
$c = substr($c,0,$commaPosition).substr($c,$spacePosition);
}
}
$splitArray = array();
foreach($days as $d){
$splitArray[$d]='';
}
foreach($combined as $c){
$daysRange = explode(' ',$c);
$daysRange = $daysRange[0];
if(strpos($daysRange,'-')===false){
$splitArray[$daysRange] = substr($c,strlen($daysRange)+1);
}
else{
$temp = explode('-',$daysRange);
$dayStart = $temp[0];
$dayStartIndex = array_search($dayStart,$days);
$dayEnd = $temp[1];
$dayEndIndex = array_search($dayEnd,$days);
for($i=$dayStartIndex;$i<=$dayEndIndex;$i++){
$splitArray[$days[$i]] = substr($c,strlen($daysRange)+1);
}
}
}
echo json_encode($splitArray);
Then use $.getJSON() to get the data.
Use a RegExp to parse the days, and then iterate through the days using the same value for each day in the set.
var hrsCondensed = ["Mon-Fri 11 am - 7:30 pm", "Sat 11 am - 6 pm"];
var hrsExpanded = ["","","","","","",""];
var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
$.each(hrsCondensed, function()
{
if (/([a-z]{3})-([a-z]{3}) (.+)/i.test(this))
{
var start = RegExp.$1;
var end = RegExp.$2;
var hrs = RegExp.$3;
var i = $.inArray(start, days);
hrsExpanded[i] = hrs;
while(days[i] != end)
{
if (++i > 6) i = 0;
hrsExpanded[i] = hrs;
}
}
else
{
var day = this.substr(0, 3);
var hrs = this.substr(4);
var i = $.inArray(day, days);
hrsExpanded[i] = hrs;
}
});
Live working example: http://jsfiddle.net/gilly3/9KztG/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With