2

I would like to have a function like PHP's version_compare() which let me compare version and consider minor/major version numbers.

My method should look like

function my_version_compare($new_version, $old_version, $operator, $only_minor = false){
    ...
    return (bool) $do_update;
}

a major version update is from 2.1 to 2.2 for instance while a minor is from 2.1 to 2.1.1

my_version_compare('2.2', '2.1', '>', false); //true
my_version_compare('2.2', '2.1', '>', true); //false
my_version_compare('2.1.1', '2.1', '>', false); //true
my_version_compare('2.1.1', '2.1', '>', true); //true

Basically I like to let the users decide if an auto update should be made on minor versions only or in any case

What I currently have works just with 2.x to 3.x updates:

function my_version_compare($new_version, $old_version, $operator, $only_minor = false){
    if($only_minor){
        return (bool) version_compare($new_version, $old_version, $operator)
                && !version_compare(intval($new_version), intval($old_version), $operator);
    }else{
        return (bool) version_compare($new_version, $old_version, $operator);
    }
}
5
  • sure thing! I just thought it would be useful for others. I've added a method I already have
    – Xaver
    Jun 2, 2015 at 9:26
  • 1
    You see, that's much better! :)
    – wonderb0lt
    Jun 2, 2015 at 9:26
  • also I like to do it "the right way". It should work with all formats version_compare() can handle and doing it from scratch is often not future proof
    – Xaver
    Jun 2, 2015 at 9:36
  • Have you looked into SemVer? Your versions looks a lot like this scheme. And there's a library for working with it!
    – wonderb0lt
    Jun 2, 2015 at 9:59
  • looks a bit complex for my needs. I may go with some custom solution
    – Xaver
    Jun 2, 2015 at 11:25

1 Answer 1

1

After some fiddling I came up with this:

function my_version_compare($new_version, $old_version, $operator, $only_minor = false){

    $pos = 1; //position to indicate what's a major version (x.[x].x.x = 1)

    if($only_minor){

        //get parts as array and remove trailing zeros
        $new = explode(".", rtrim($new_version, ".0"));
        $old = explode(".", rtrim($old_version, ".0"));

        //check if it's a major version update
        $is_major_update = version_compare($new[$pos], $old[$pos], $operator) || version_compare(intval($new_version), intval($old_version), $operator);

        //check if it's a minor update
        $is_minor_update = (!$is_major_update && version_compare(strstr($new_version, '.'), strstr($old_version, '.'), $operator));

        return $is_minor_update;
    }

    return version_compare($new_version, $old_version, $operator);

}

Feel free to improve this snippet!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.