Composer Command Line Interface and Commands(Part 1)


In the previous tutorials we have already used the command line to punch some composer commands, and get some things done using composer, in this tutorial, we will look at all the available composer commands and their uses.

In the command line, to get or to access these composer commands, simply type “composer” or “composer list” to see a complete list of composer commands. The “—help” flag gives you more details about a command.

Composer makes use of Symfony/console, this allows us to call commands by their short name, if they are not ambiguous as shown in the snippet below:

composer dump

The above code snippet is a short form of the composer command “composer auto-dump”

Composer Commands

#Global Options:

 The global options are available on every composer command. They are used to further describe a command as shown below:

composer --help

The above command will display all the commands in the composer, just because of the global option

“ --help”.

Other Global Options are:
  • --verbose (-v): This increases the  verbosity of messages.
  • --help (-h): This command displays help information.
  • --quiet (-q): The do not output any message command.
  • --no-interaction (-n): The do not ask any interactive question.
  • --no-plugins: This disables all plugins.
  • --no-cache: this disables the use of the cache directory.
  • --working-dir (-d): If specified, it forces composer to use the given directory as working directory.
  • --profile: This display timing and memory usage information
  • --ansi: Used to force ANSI output.
  • --no-ansi: Used to disable ANSI output.
  • --version (-V): It displays this application version.

#Process Exit Codes

Process exit codes are codes generally used to exit a process, they include

  • 0: OK, when a process completes successfully
  • 1: Generic/unknown error code
  • 2: Dependency solving error code


The init command is used to initialize composer in a project. This is shown in the following snippet below

composer init

This command has the following options:

  • name: This is the name of the package.
  • description: A short description about of the package.
  • author: The package owners name.
  • type: Defines the type of package.
  • homepage: The URL homepage of the package.
  • require: Some packages to require a version constraint. It should be in format “proj/paa:1.0.0”
  • require-dev: Packages required for development
  • stability (-s): A value for the minimum-stability field.
  • license (-l): The type of license the package has.
  • repository: Where can the package be located. A repository specifies where a package is hosted.

#install / i

The “install” command reads the “composer.json” file from the projects root  directory, then resolves the dependencies, and installs them into the “vendor” folder.

composer install

In the presence of a “composer.lock” file in the current directory, it will use the exact versions from there instead of resolving them. This ensures that everyone using the package will get the same versions of the dependencies.

If there is no “composer.lock” file, Composer will create one after dependency resolution.

The “composer install” command has the following optional parameters:

  • --prefer-source: Packages can be downloaded in two way: source and dist. The Composer uses the dist by default for stable versions of packages. The source is used to download packages from version control systems.
    composer install  --prefer-source
    In the above code snippet, the composer will download from the version control source if there is one. This is usually used for bug fixes.
  • --prefer-dist: This is the opposite of --prefer-source, Composer will install directly from Packagist if possible. This is the default composer behavior. Thus, the command “composer install” and “composer install –prefer-dist” gives the same result.
  • --dry-run: This command allows you to simulate an installation process without installing a package.
    composer install --dry-run
  • --dev: This command installs packages listed in the “require-dev” block of the composer.json file. This is actually the composer default behavior.
  • --no-dev: This command causes composer to skip installing packages listed in require-dev. Also, the autoloader generation skips the autoload-dev rules.
  • --no-autoloader: This installs the packages without generating the autoloader file.
  • --no-scripts: This command skips the execution of scripts defined in composer.json.
  • --no-progress: This removes the progress display that a times  mess with some terminals or scripts where backspaces are not allowed.
  • --no-suggest: This installs the packages without giving suggestions to other recommended packages.
  • --optimize-autoloader (-o): Convert PSR-0/4 autoloading to class map to get an optimized autoloader. This is recommended for the production environment, as takes quite some time to complete, so this is not a composer default behavior.
  • --classmap-authoritative (-a): This autoload classes from the classmap thereby. implicitly enabling --optimize-autoloader.
  • --apcu-autoloader: This uses APCu to cache found/not-found classes.
  • --ignore-platform-reqs: This forces composer to ignore system requirements for packages. Thus, it ignores packages even when the requirements are not met.
    composer install -–ignore-platform-reqs

Previous: The composer.json schema
Next: Composer command line interface and commands (Part 2)

PHP: Tips of the Day

How to calculate the difference between two dates using PHP?

Use this for legacy code (PHP < 5.3). For up to date solution see jurka's answer below

You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it's rather easy to calculate different time periods.

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

Edit: Obviously the preferred way of doing this is like described by jurka below. My code is generally only recommended if you don't have PHP 5.3 or better.

Several people in the comments have pointed out that the code above is only an approximation. I still believe that for most purposes that's fine, since the usage of a range is more to provide a sense of how much time has passed or remains rather than to provide precision - if you want to do that, just output the date.

Despite all that, I've decided to address the complaints. If you truly need an exact range but haven't got access to PHP 5.3, use the code below (it should work in PHP 4 as well). This is a direct port of the code that PHP uses internally to calculate ranges, with the exception that it doesn't take daylight savings time into account. That means that it's off by an hour at most, but except for that it should be correct.


 * Calculate differences between two dates with precise semantics. Based on PHPs DateTime::diff()
 * implementation by Derick Rethans. Ported to PHP by Emil H, 2011-05-02. No rights reserved.
 * See here for original code:
 * http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/tm2unixtime.c?revision=302890&view=markup
 * http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/interval.c?revision=298973&view=markup

function _date_range_limit($start, $end, $adj, $a, $b, $result)
    if ($result[$a] < $start) {
        $result[$b] -= intval(($start - $result[$a] - 1) / $adj) + 1;
        $result[$a] += $adj * intval(($start - $result[$a] - 1) / $adj + 1);

    if ($result[$a] >= $end) {
        $result[$b] += intval($result[$a] / $adj);
        $result[$a] -= $adj * intval($result[$a] / $adj);

    return $result;

function _date_range_limit_days($base, $result)
    $days_in_month_leap = array(31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    $days_in_month = array(31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    _date_range_limit(1, 13, 12, "m", "y", &$base);

    $year = $base["y"];
    $month = $base["m"];

    if (!$result["invert"]) {
        while ($result["d"] < 0) {
            if ($month < 1) {
                $month += 12;

            $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0);
            $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month];

            $result["d"] += $days;
    } else {
        while ($result["d"] < 0) {
            $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0);
            $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month];

            $result["d"] += $days;

            if ($month > 12) {
                $month -= 12;

    return $result;

function _date_normalize($base, $result)
    $result = _date_range_limit(0, 60, 60, "s", "i", $result);
    $result = _date_range_limit(0, 60, 60, "i", "h", $result);
    $result = _date_range_limit(0, 24, 24, "h", "d", $result);
    $result = _date_range_limit(0, 12, 12, "m", "y", $result);

    $result = _date_range_limit_days(&$base, &$result);

    $result = _date_range_limit(0, 12, 12, "m", "y", $result);

    return $result;

 * Accepts two unix timestamps.
function _date_diff($one, $two)
    $invert = false;
    if ($one > $two) {
        list($one, $two) = array($two, $one);
        $invert = true;

    $key = array("y", "m", "d", "h", "i", "s");
    $a = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $one))));
    $b = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $two))));

    $result = array();
    $result["y"] = $b["y"] - $a["y"];
    $result["m"] = $b["m"] - $a["m"];
    $result["d"] = $b["d"] - $a["d"];
    $result["h"] = $b["h"] - $a["h"];
    $result["i"] = $b["i"] - $a["i"];
    $result["s"] = $b["s"] - $a["s"];
    $result["invert"] = $invert ? 1 : 0;
    $result["days"] = intval(abs(($one - $two)/86400));

    if ($invert) {
        _date_normalize(&$a, &$result);
    } else {
        _date_normalize(&$b, &$result);

    return $result;

$date = "1986-11-10 19:37:22";

print_r(_date_diff(strtotime($date), time()));
print_r(_date_diff(time(), strtotime($date)));

Ref : https://bit.ly/3gVZuyT