Php script that determines how many years of organization


28-08-2018
Денис Л.
Php
Php script that determines how many years of organization

Today I was tasked to change the number of years that were performed by our organization in the slider on the main page. In particular, the slider contained the phrase "22 years on the market of expert services." Therefore, it was necessary to do 23. Like any self-respecting programmer, I am a person who automates all routine processes. And especially those that you can forget. After all, it's not so easy to remember every year that on a certain date you need to change one digit in one place in the code.

And I wrote a script that defines the current date, counts how many years of the organization, and displays everything in the light of the case of the Russian language.

I tested the script on any dates that deduce less than 1 year and up to 110 years.

In particular, the line of text now looks like this:

<?=getFullExperience()?> in the market of expert services

And the line of code displays in our case, at the current time: "23 years on the market of expert services."

When the company is 25 years old, it will be deduced: "25 years ..." and so on.

So, how to automate the calculation of years for the organization (or person)

First of all, let us pay attention to the peculiarities of the cases of the English language in relation to the years. In particular, any number ending in '1' (except 11) will be read as 'year'. All other numbers are read as 'years'. In a special way, only the number '11' is read, which ends with '1', but is always read as 'years'.

Below is a script, with comments.


<?
    function getFullExperience() {

        // introduce the date of foundation of the company. For example, I introduced my date of birth
        // To avoid errors when entering digits with leading zero, for example, '05',
        // We take numbers as strings and convert them to numbers using intval()
        $dayBirth = intval('29');
        $monthBirth = intval('05');
        $yearBirth = intval('1983');

        // determine the current year, month, day
        $dayNow = date('d'); $monthNow = date('m'); $yearNow = date('Y');

        // if the current month is more than the month of the company's establishment
        // or if the current month is equal to the base month and the current day is greater than or equal to the base day
        if($monthNow > $monthBirth || ($monthNow == $monthBirth && $dayNow >= $dayBirth)) {
            $fullExperience = $yearNow - $yearBirth; // the number of years is equal to the current year minus the year of foundation
        }
        else { // otherwise, the number of years is less by 1
            $fullExperience = $yearNow - $yearBirth - 1;
        }

        // further we form the text of 'year' or 'years'
        // if the number of years is 11, we will always add the word 'years'
        if($fullExperience == 11) {
            $dayText = 'years';
        }
        else { // for all other numbers
            // write to variable $dayText the last number in the set of digits
            // ie, if the number consists of two digits, we write down the last
            // If the number is from one digit - then it will be the last
            // To do this, we first convert the number of experiments into a string using the function strval()
            // then take the last character of the string, for example: $fullExperience[1]
            $dayText = strval($fullExperience)[strlen($fullExperience)-1];
            if($dayText == 1) {
                $dayText = 'year';
            }
            else {
                $dayText = 'years';
            }
        }

        return $fullExperience.' '.$dayText;
    }
?>

Example of calling a function in html code:

<p>Our company is already working <?=getFullExperience()?> in the market of expert services</p>

Will be displayed, for example: 'Our company has been working for 23 years in the market of expert services'


By the way, for fans of object-oriented style there is still such a variant of calculating the date:

<?
$now = new DateTime();
$company_birth = new DateTime('29-05-1983');
$diff = date_diff($now, $company_birth);
echo $diff->format('%y');
?>

I will be glad if your site comes in handy with this script!