Skip to content

StrtotimeUsage ​

warning on by default quick-fix

Group: Performance · PhpStorm name: StrtotimeUsageInspection

Two wasteful strtotime() idioms: parsing the literal 'now' just to get the current timestamp (time() does that directly), and passing time() as the base timestamp, which is already the default.

Example ​

php
<?php
namespace Billing;

function due($spec) {
    $now   = strtotime("Now");
    $other = \strtotime('NOW');
    $next  = strtotime($spec, \time());
    $last  = strtotime('last monday', time());

    $a = strtotime('now ');
    $b = strtotime($spec, $now);
    $c = strtotime($spec, microtime(true));
    $d = strtotime('now', time());
    $e = strtotime();
    return [$now, $other, $next, $last, $a, $b, $c, $d, $e];
}
php
<?php
namespace Billing;

function due($spec) {
    $now   = time();
    $other = \time();
    $next  = strtotime($spec);
    $last  = strtotime('last monday');

    $a = strtotime('now ');
    $b = strtotime($spec, $now);
    $c = strtotime($spec, microtime(true));
    $d = strtotime('now');
    $e = strtotime();
    return [$now, $other, $next, $last, $a, $b, $c, $d, $e];
}

Reported:

  • line 5: Call time() instead of parsing 'now'.
  • line 6: Call time() instead of parsing 'now'.
  • line 7: The base timestamp already defaults to the current time; drop the time() argument.
  • line 8: The base timestamp already defaults to the current time; drop the time() argument.

Configure ​

In custos.json:

json
{
  "rules": {
    "StrtotimeUsage": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore StrtotimeUsage

/**
 * @noinspection StrtotimeUsageInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.