herrera-io/date-interval
Extends PHP’s DateInterval with handy conversions: turn intervals into ISO interval specs and seconds, and recreate intervals from seconds. Useful for normalizing and serializing durations (e.g., P2H <-> 7200).
DateInterval) in Laravel applications where native DateInterval lacks flexibility. It aligns with Laravel’s reliance on DateInterval for time-based logic (e.g., scheduling, billing) but extends it with serialization/deserialization capabilities critical for APIs, databases, or cron jobs.DateInterval usage in legacy systems or low-level operations (e.g., PostgreSQL INTERVAL queries).DateInterval logic (e.g., in Illuminate\Bus\PendingDispatch) risks breaking changes due to the package’s archived status and lack of Laravel-specific optimizations.DateInteval (README example) and reliance on ext-bcmath may require patches or fallbacks (e.g., gmp extension).CarbonInterval extensions).INTERVAL syntax or raw SQL queries where DateInterval is directly used.toSeconds()/toSpec() against edge cases (e.g., P1DT2H3M4.5S, negative intervals).ext-bcmath: Not enabled by default in Laravel stacks; may need Dockerfile or php.ini configuration.add(), sub(), or diff().toSeconds() may lose accuracy for fractional seconds or intervals > 292 years (PHP’s DateInterval limit).Why Not Carbon or Spatie?
DateInterval handling for your use case?Migration Strategy
DateInterval usage in Laravel’s core (e.g., schedule:run, bus:dispatch)?DateInterval facade be needed to maintain consistency?Failure Modes
bcmath is disabled? (e.g., gmp or bcmath polyfill?)toSeconds()?Long-Term Viability
spatie/laravel-date or carbon/carbon)?spatie/laravel-date-interval) that could be leveraged?DateInterval to/from ISO-8601 for OpenAPI specs or GraphQL inputs.schedule:run with custom interval parsing (e.g., * * * * * command:run --interval="P1D").INTERVAL syntax for raw SQL queries (e.g., PostgreSQL INTERVAL '2 hours').P1M) to seconds for payment processing.date-fns, luxon).createFromFormat() is more robust for parsing.addDays(), subMonths().Pilot Phase (Low Risk):
namespace App\Extensions;
use Herrera\DateInterval\DateInterval as HerreraDateInterval;
use Carbon\CarbonInterval;
class DateIntervalExtension extends HerreraDateInterval {
public static function fromCarbon(CarbonInterval $interval): self {
return new self($interval->spec);
}
public function toCarbon(): CarbonInterval {
return CarbonInterval::createFromSpec($this->toSpec());
}
}
bcmath:
{
"scripts": {
"post-install-cmd": "php -r \"if (!extension_loaded('bcmath')) { echo 'ERROR: bcmath extension required!'; exit(1); }\""
}
}
Core Integration (Medium Risk):
$this->app->bind(\DateInterval::class, function ($app) {
return new \Herrera\DateInterval\DateInterval(...);
});
Interval class to use this package’s methods:
CarbonInterval::macro('toSeconds', function () {
return app(\DateInterval::class)->fromCarbon($this)->toSeconds();
});
DateInterval in schedule:run with the new wrapper.Testing (Critical):
$this->assertEquals(189600, (new DateIntervalExtension('P2DT3H'))->toSeconds());
$this->assertEquals('P2DT3H', (new DateIntervalExtension('P2DT3H'))->toSpec());
Gradual Rollout (High Risk):
DateInterval in new features.DateInterval logic (e.g., in App\Jobs\*, App\Console\Commands\*).DateInteval → DateInterval (class name in the example).bcmath is enabled (fallback to gmp if needed).json_encode()).How can I help you explore Laravel packages today?