承接 ed9/laravel-date-directive 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ed9/laravel-date-directive

最新稳定版本:1.0.2

Composer 安装命令:

composer require ed9/laravel-date-directive

包简介

Simplify the date & time handling when in need to show date & time to customer in their timezone.

README 文档

README

Overview:

Laravel Date Directive is a comprehensive Laravel library designed to address persistent challenges related to site content, user interactions, and regional settings. It facilitates the seamless presentation of date and time elements based on user timezones. This library extends on-top of the laravel-readable library.

Installation:

To integrate the library into your Laravel project, use Composer:

composer require ed9/laravel-date-directive

Configuration:

To customize the library behavior, publish the configuration file:

php artisan vendor:publish --tag=date-time-directive

Configuration options include:

Key Type Description
always_show_seconds boolean When the library outputs time, it includes seconds if set to true.
timezone string A valid timezone name; refer to the list of supported timezones.
string_format string Supports \Carbon\Carbon or string input; if string is used, it must match the configured format.
use_12_hour_clock boolean When the library outputs time, it displays the time in 12-hour format if set to true, or 24-hour format if set to false.

Setup:

Configure the system default settings in the configuration file. Optionally, configure per-user settings by adding timezone and other configuration items to your users table. Update the \App\Http\Middleware\Authenticate::class by extending the boot method, overriding the library configuration during runtime.

public function handle($request, Closure $next, ...$guards)
{
    if ($user = auth()->user()) {
        $handler = app(\Ed9\LaravelDateDirective\Handler::class);
        $handler->set12HourFormat($user->show_time_with_12_hours);
        $handler->setTimezone($user->timezone);
    }

    return parent::handle($request, $next, ...$guards);
}

Usage:

In Blade Templates:

Directive Arguments Output
@time \Carbon\Carbon, string Outputs the time of the input in the configured timezone.
@date \Carbon\Carbon, string Outputs the date of the input in the configured timezone.
@dateTime \Carbon\Carbon, string Outputs the date and time of the input in the configured timezone.

Example:

User {{ $user->name}} was created at @time($user->created_at) on @date($user->created_at).
Some might say, this was on @dateTime($user->created_at)!

In PHP Code:

If you need access to the formatted and converted values in your PHP code, use the \Ed9\LaravelDateDirective\Handler:

$handler = app(\Ed9\LaravelDateDirective\Handler::class);
echo 'User ' . $user->name . ' was created at ' . $handler->time($user->created_at).' on '.$handler->date($user->created_at) . '.';
echo 'Some might say, this was on ' . $handler->dateTime($user->created_at) . '!';

Available Methods via Handler:

  • set12HourFormat(boolean): self: Configures the library to return time in 12 or 24-hour format.
  • setInputDateTimeFormat(string): self: Configures the expected input format when calling directives using string values.
  • setTimezone(string): self: Configures the timezone to which all output should be converted.
  • setUseSeconds(boolean): self: Configures the library to return time with or without seconds.
  • time(\Carbon\Carbon|string): string: Converts provided input into a time string in the configured timezone.
  • date(\Carbon\Carbon|string): string: Converts provided input into a date string in the configured timezone.
  • dateTime(\Carbon\Carbon|string): string: Converts provided input into a datetime string in the configured timezone.

统计信息

  • 总下载量: 192
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-07