定制 tonysm/local-time-laravel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

tonysm/local-time-laravel

Composer 安装命令:

composer require tonysm/local-time-laravel

包简介

A port of the `local-time` gem from Basecamp.

README 文档

README

Logo Local Time Laravel

Total Downloads License

This is a Laravel port of the local_time gem from Basecamp. It makes it easy to display date and time to users in their local time. Its Blade components render a time HTML tag in UTC (making it cache friendly), and the JavaScript component immediately converts those elements from UTC to the Browser's local time.

Installation

  1. Install the package via Composer:
composer require tonysm/local-time-laravel
  1. Install the local-time JS lib via NPM:
npm install local-time -D

And then import it on your resources/app.js file, like so:

// ...
import LocalTime from "local-time"
LocalTime.start()

Usage

This package adds a couple Blade components to your project, they are:

<x-local-time::time :value="now()" />

Formats the Carbon instance using the default format string. It will convert the regular PHP formats to the strftime format for you.

<x-local-time::time :value="now()" format="F j, Y g:ia" />

Alias for <x-local-time::time /> with a month-formatted default. It converts that format to %B %e, %Y %l:%M%P.

<x-local-time::date :value="now()" format="F j, Y" />

You can configure the format used by passing it as a prop to the component. Any other attribute will be rendered in the generated time tag.

<x-local-time::time :value="now()" class="my-time" />

Renders the time tag using the default time format and adds the given class tag attribute to the element.

Note: The included strftime JavaScript implementation is not 100% complete. It supports the following directives: %a %A %b %B %c %d %e %H %I %l %m %M %p %P %S %w %y %Y %Z

Time ago helper

<x-local-time::ago :value="now()" />

Displays the relative amount of time passed. With age, the descriptions transition from {quantity of seconds, minutes, or hours} to {date + time} to {date}. The <time> elements are updated every 60 seconds.

Examples (in quotes):

  • Recent: "a second ago", "32 seconds ago", "an hour ago", "14 hours ago"
  • Yesterday: "yesterday at 5:22pm"
  • This week: "Tuesday at 12:48am"
  • This year: "on Nov 17"
  • Last year: "on Jan 31, 2012"

Relative time helper

Preset time and date formats that vary with age. The available types are date, time-ago, time-or-date, and weekday. Like the <x-local-time::time /> component, type can be passed a string. The default type is time-or-date.

<x-local-time::relative :value="now()" type="weekday" />
<x-local-time::relative :value="now()" type="time-or-date" />

Available type options:

  • date: Includes the year unless it's current. "Apr 11" or "Apr 11, 2013"
  • time-ago: See above. <x-local-time::ago /> calls <x-local-time::relative /> with this type option.
  • time-or-date: Displays the time if it occurs today or the date if not. "3:26pm" or "Apr 11"
  • weekday: Displays "Today", "Yesterday", or the weekday (e.g. Wednesday) if the time is within a week of today.
  • weekday-or-date: Displays the weekday if it occurs within a week or the date if not. "Yesterday" or "Apr 11"

Example

php artisan tinker
>>> $user->created_at
=> Illuminate\Support\Carbon @1625103168 {#4106
     date: 2021-06-30 22:32:48.0 UTC (+00:00),
   }
<x-local-time::time :value="$user->created_at" />

Renders:

<time data-format="%B %e, %Y %l:%M%P"
      data-local="time"
      datetime="2021-06-30T22:32:48Z">June 30, 2021 22:32pm</time>

And is converted client-side to:

<time data-format="%B %e, %Y %l:%M%P"
      data-local="time"
      datetime="2021-06-30T22:32:48Z"
      title="June 30, 2013 22:32pm EDT"
      data-localized="true">June 30, 2021 22:32pm</time>

Empty Placeholder

When a null value is passed to <x-local-time::time> or <x-local-time::date>, the component renders an empty placeholder string (default ' -- ') instead of a formatted date:

<x-local-time::time :value="$user->deleted_at" />
{{-- Renders " -- " if deleted_at is null --}}

You can customize the placeholder globally (see Configuration below).

Configuration

To configure the default date and time formats, you can use the useTimeFormat and useDateFormat methods on the LocalTimeLaravelFacade on your AppServiceProvider, like so:

<?php

use Illuminate\Support\ServiceProvider;
use Tonysm\LocalTimeLaravel\LocalTimeLaravelFacade;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        LocalTimeLaravelFacade::useTimeFormat('H:i');
        LocalTimeLaravelFacade::useDateFormat('d/m/Y H:i');
        LocalTimeLaravelFacade::useEmptyPlaceholder('N/A');
    }
}

The JavaScript lib allows some configurations as well as internationalization (i18n). To know more about that, head out to the Rails gem documentation.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email tonysm@hey.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

tonysm/local-time-laravel 适用场景与选型建议

tonysm/local-time-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 900 次下载、GitHub Stars 达 57, 最近一次更新时间为 2021 年 07 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「tonysm」 「local-time-laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tonysm/local-time-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tonysm/local-time-laravel 我们能提供哪些服务?
定制开发 / 二次开发

基于 tonysm/local-time-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 57
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-01