adlawson/timezone
Sets PHP’s default timezone to UTC when date.timezone isn’t configured, preventing DateTime “not safe to rely on system timezone” warnings. Auto-runs on include (side effect), intended for end-user apps/frameworks, not libraries.
Installation:
composer require adlawson/timezone:1.0.0
Add the autoloader to your entry point (e.g., index.php or bootstrap/app.php):
require __DIR__.'/vendor/autoload.php';
First Use Case:
DateTime timezone warning (e.g., during php artisan serve or API requests), this package silently enforces UTC as the default timezone, eliminating the warning.php artisan tinker
>>> new DateTime(); // No warning, defaults to UTC
date_default_timezone_get() returns UTC after autoloading.Framework-Agnostic Initialization:
require __DIR__.'/vendor/autoload.php'; before any DateTime usage in your Laravel app’s entry point (e.g., public/index.php or bootstrap/app.php).bootstrap/app.php:
require __DIR__.'/../vendor/autoload.php'; // <-- Add this line early
$app = new Application();
Testing Timezone Behavior:
date_default_timezone_get() in tests to verify UTC is enforced:
use function PHPUnit\Framework\assertEquals;
public function testTimezoneIsUTC()
{
assertEquals('UTC', date_default_timezone_get());
}
Avoiding Side Effects:
Debugging Timezone Warnings:
php.ini for date.timezone overrides.date_default_timezone_set()).error_reporting(E_ALL) to confirm the warning is suppressed.Laravel-Specific Integration:
config/app.php timezone setting (if present) by placing the autoloader before Laravel boots:
// bootstrap/app.php
require __DIR__.'/../vendor/autoload.php'; // Force UTC first
$app = new Application();
Fallback for Dynamic Environments:
php.ini isn’t configurable:
# docker-compose.yml
services:
app:
image: php:8.2-cli
volumes:
- ./vendor:/var/www/vendor
command: php -d date.timezone=UTC script.php # Redundant but explicit
composer.json to auto-enforce UTC during testing:
"scripts": {
"test": "php -d date.timezone=UTC vendor/bin/phpunit",
"pre-autoload-dump": "php -d date.timezone=UTC vendor/bin/composer dump-autoload"
}
boot() method:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class TimezoneProvider extends ServiceProvider
{
public function boot()
{
if (date_default_timezone_get() === false) {
date_default_timezone_set('UTC');
}
}
}
(Note: This is redundant if the package is already loaded, but useful for explicit control.)PSR-1 Violation:
date.timezone is untouched.Redundant Overrides:
date.timezone is already set in php.ini or via ini_set(), the package’s check may still run but won’t override. Test with:
php -d date.timezone=America/New_York -r "require 'vendor/autoload.php'; echo date_default_timezone_get();"
(Should output UTC, not America/New_York.)Laravel Caching:
config_cache, the autoloader may run once during boot. For dynamic environments, ensure the package is loaded before Laravel caches configurations.Timezone Database Updates:
Verify Execution:
date_default_timezone_get() in a Laravel Tinker session:
>>> date_default_timezone_get();
"UTC"
UTC, the autoloader may not have executed (e.g., cached OPcode).Suppress Warnings:
bootstrap/app.php after autoloading:
require __DIR__.'/../vendor/autoload.php';
date_default_timezone_set('UTC'); // Explicit fallback
Check for Conflicts:
vlucas/phpdotenv) might also set timezones. Use strace or xdebug to trace execution order:
strace -e trace=file php artisan tinker 2>&1 | grep timezone
Explicit Overrides:
date.timezone in php.ini or .env (Laravel) to avoid relying on the package:
; php.ini
date.timezone = UTC
or
# .env
APP_TIMEZONE=UTC
Testing Edge Cases:
date.timezone unset in php.ini:
php -n -r "require 'vendor/autoload.php'; echo date_default_timezone_get();"
(Should output UTC.)Alternative for Libraries:
date.timezone in their composer.json or documentation:
"config": {
"preferred-install": "dist",
"timezone": "UTC" // Hint for users
}
Performance Note:
// bootstrap/app.php
if (function_exists('date_default_timezone_get') && !date_default_timezone_get()) {
date_default_timezone_set('UTC');
}
Forking for Custom Logic:
// Custom timezone.php
require '/path/to/adlawson/timezone/lib/timezone.php';
if (date_default_timezone_get() === 'UTC') {
error_log('UTC enforced by timezone package');
}
How can I help you explore Laravel packages today?