3

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] => 
[
] => 
)
3
  • 1
    You have some code in place? Did you try something on it? Commented Jun 28, 2011 at 22:26
  • I'm kind of stuck, with a huge amount of if/else statements considering cases Mon-Tue, Mon-Wed, Mon-Thu, Mon-Fri, Mon-Sat, Mon-Sun, Tue-Wed, Tue-Thu... etc Commented Jun 28, 2011 at 22:28
  • 1
    Think about splitting that string with a - and then approaching the problem. Commented Jun 28, 2011 at 22:31

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thats great! Now I have a string as shown in the updated question above, and the code now require to consider the comma...
I'm thinking the tweak has to be at $daysRange and an explode with ', ' as the delimiter, and an additional elseif condition elseif(strpos($daysRange,'-') == true && strpos($daysRange,', ') == true), then I'm stuck :(
@Nyxynyx just added a loop to change the ones with comma to older format. This should work.
1

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/

Comments

0

Use regular expressions!

Example (quick and dirty):

<html>
<body>

<script type="text/javascript">
var a = [];
a.push("Mon-Fri 11 am - 7:30 pm");
a.push("Sat 11 am - 6 pm");

var seeker = /(\d{1,2}|\d{1,2}:\d{1,2}) (am|pm)/g;
for (var i = 0; i < a.length; ++i) {
    document.write(a[i] + " ==> ");
    document.write(a[i].match(seeker) + "<br>");
}
</script>

</body>
</html>

Put this code in a sandbox and You will get:

Mon-Fri 11 am - 7:30 pm ==> 11 am,7:30 pm
Sat 11 am - 6 pm ==> 11 am,6 pm

Sources: String.match(), JavaScript Regexp

Comments

0

This should get you started.

var dates = ['Mon-Fri 11 am - 7:30 pm','Sat 11 am - 6 pm'];//for testing
    var regex = '(\\w{3})-?(\\w{3})?\\s+(.*)';
    var week = {'Mon':0, 'Tue':1, 'Wed':2, 'Thu':3, 'Fri':4, 'Sat':5, 'Sun':6};
    function foo(){
        var datum = [];
        re = new RegExp(regex);
        for(i in dates){
            match = re.exec(dates[i]);
            if(match != null){
                from = week[match[1]];
                to = match[2]? week[match[2]]: from;
                tim = match[3];

                for(i=from; i<=to; ++i){
                    datum[i] = tim;
                }
            }
        }

        alert(datum);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.