Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:44:19+00:00 2026-05-22T01:44:19+00:00

I am trying to compact an expression of individual days into a shorter expression

  • 0

I am trying to compact an expression of individual days into a shorter expression including hyphen-separate ranges.

Examples:

  • mon,tue,wed,thu,fri,sat
    to be:
    mon-sat
  • mon,tue,wed,fri,sat
    to be
    mon-wed,fri-sat

My coding attempt:

function dayrange($days){
    $days = explode(",", str_replace(" ","",$days));
    return reset($days) . "-" . end($days);
}

How can I shorten the multi-day expression so that consecutive days are merged into a range of days?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:44:20+00:00Added an answer on May 22, 2026 at 1:44 am

    Basically, I would approach this by:

    1. Converting the days to corresponding numeric values
    2. Turning the array of numbers into a string with ranges
    3. Converting the numbers in the string back into days of the week

    I wrote some code to do that:

    /**
     * Convert an array of numbers to a string containing ranges and single values
     * @param array $numbers an array of numbers
     * @return string
     */
    function compressNumbers($numbers) {
        $result = array();
        sort($numbers);
        $previousValue = reset($numbers);
        $startValue = $previousValue;
        foreach ($numbers as $value) {
            if ($value > $previousValue + 1) {
                if ($startValue == $previousValue) {
                    $result[] = $startValue;
                } else {        
                    $result[] = $startValue . '-' . $previousValue;
                }
                $startValue = $value;
            }
            $previousValue = $value;
        }
        if ($startValue == $previousValue) {
            $result[] = $startValue;
        } else {        
            $result[] = $startValue . '-' . $previousValue;
        }
        return implode(',', $result);
    }
    
    /*
     * Creates an array with values the three letter representation for days of the 
     * week and keys the corresponding numeric representation.
     *
     * @return array
     */
    function createLookupNumberToDay() {
        $date = strtotime('now');
        $lookup = array();
        for ($i = 1; $i <= 7; $i++) {
            $lookup[date('w', $date)] = date('D', $date);
            $date = strtotime('+1 day', $date);
        }
        return $lookup;
    }
    
    /*
     * Converts a string listing days separated by commas into 
     * an array with values the numeric value for the corresponding
     * day of the week.
     *
     * @param string $days
     * @return array
     */
    function convertDaysToNumbers($days) {
        $result = array();
        $daysArray = explode(",", str_replace(" ","",$days));
        foreach ($daysArray as $day) {
            $result[] = date('w', strtotime($day));
        }
        return $result;
    }
    
    /*
     * Converts the numbers in a string to the corresponding 3-letter day of the
     * week abbreviation.
     *
     * @param string $string
     * @return string
     */
    function convertNumbersToDays($string) {
        $lookup = createLookupNumberToDay();
        return str_replace(array_keys($lookup), $lookup, $string);
    }
    
    function convert($string) {
        return (convertNumbersToDays(compressNumbers(convertDaysToNumbers($string))));
    }
    
    echo convert('mon,tue,wed,thu,fri,sat');
    echo '<br />';
    echo convert('mon,tue,wed,sat');
    echo '<br />';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the .NET Compact Framework 3.5 and I am trying to determine
Here's my problem. I'm currently trying to develop a .Net Compact Framework 2.0 application
Trying to use nhibernate on sql-server compact edition for unit testing and having some
I am trying to validate against a RelaxNG compact syntax file using Nokogiri. Nokogiri
I'm trying to open an SQL Compact database in SQL Management studio. If the
I'm trying to write a compact update controller for CRUD activity. Here is the
I'm trying to use an 'SQL Express' (or is it SQL Compact?) .MDF file
I'm trying to set up some unit tests for an existing compact framework class
I am using SqlServer Compact 3.5 (2008) and am trying to return rows that
I'm looking for an IoC container to use in my Compact Framework application. Trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.