This is a Laravel port of the local_time gem from Basecamp.

Overview

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 :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 :value="now()" format="F j, Y g:ia" />

Alias for with a month-formatted default. It converts that format to %B %e, %Y %l:%M%P.

">
<x-local-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 :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 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 component, type can be passed a string.

">
<x-local-relative-time :value="now()" type="weekday" />
<x-local-relative-time :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. calls 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),
   }
created_at" /> ">
<x-local-time :value="$user->created_at" />

Renders:

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

And is converted client-side to:

June 30, 2021 22:32pm ">
<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:32pmtime>

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:



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');
    }
}

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 [email protected] 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.

You might also like...
Laravel Sanctum support for Laravel Lighthouse
Laravel Sanctum support for Laravel Lighthouse

Lighthouse Sanctum Add Laravel Sanctum support to Lighthouse Requirements Installation Usage Login Logout Register Email Verification Forgot Password

Bring Laravel 8's cursor pagination to Laravel 6, 7

Laravel Cursor Paginate for laravel 6,7 Installation You can install the package via composer: composer require vanthao03596/laravel-cursor-paginate U

A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic! concepts like , Hijri Dates & Arabic strings and so on ..

Jetstrap is a lightweight laravel 8 package that focuses on the VIEW side of Jetstream / Breeze package installed in your Laravel application

A Laravel 8 package to easily switch TailwindCSS resources generated by Laravel Jetstream and Breeze to Bootstrap 4.

Laravel Jetstream is a beautifully designed application scaffolding for Laravel.

Laravel Jetstream is a beautifully designed application scaffolding for Laravel. Jetstream provides the perfect starting point for your next Laravel application and includes login, registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management.

Laravel Larex lets you translate your whole Laravel application from a single CSV file.
Laravel Larex lets you translate your whole Laravel application from a single CSV file.

Laravel Larex Translate Laravel Apps from a CSV File Laravel Larex lets you translate your whole Laravel application from a single CSV file. You can i

A Laravel package that adds a simple image functionality to any Laravel model
A Laravel package that adds a simple image functionality to any Laravel model

Laraimage A Laravel package that adds a simple image functionality to any Laravel model Introduction Laraimage served four use cases when using images

A Laravel extension for using a laravel application on a multi domain setting
A Laravel extension for using a laravel application on a multi domain setting

Laravel Multi Domain An extension for using Laravel in a multi domain setting Description This package allows a single Laravel installation to work wi

Comments
  • Use Blade components instead of directives

    Use Blade components instead of directives

    I think we could have something like this:

    <x-local-time :value="$user->created_at" />
    <x-local-date :value="$user->created_at" />
    <x-local-time ago :value="$user->created_at" />
    <x-local-time relative="weekday" :value="$user->created_at" />
    

    We can use the formats:

    <x-local-time format="m/d/Y H:i:s" :value="$user->created_at" />
    
    opened by tonysm 1
Releases(1.1.0)
Owner
Tony Messias
Tony Messias
Asset Component is a port of Laravel 3 Asset for Orchestra Platform.

Asset Component is a port of Laravel 3 Asset for Orchestra Platform. The component main functionality is to allow asset declaration to be handle dynamically and asset dependencies can be resolve directly from the container. It however is not intended to becoma an asset pipeline package for Laravel, for such purpose we would recommend to use Grunt or Gulp.

Orchestra Platform 54 Mar 31, 2022
Magewire is a Laravel Livewire port for Magento 2.

Magewire is a Laravel Livewire port for Magento 2. The goal is to make it fun and easy to build modern, reactive and dynamic interfaces, without leaving the comfort of Magento's core layout and template systems. Magewire can be the missing piece when you intend to build dynamic and reactive features, but don't require or feel comfortable working with a full JavaScript framework like Vue or React.

Magewirephp 120 Jan 2, 2023
Adds phone number functionality to Laravel based on the PHP port of Google's libphonenumber API by giggsey.

Laravel Phone Adds phone number functionality to Laravel based on the PHP port of Google's libphonenumber API by giggsey. Table of Contents Demo Insta

null 2.1k Jan 2, 2023
PHP port of Underscore.js

Underscore.php Underscore.php is a PHP port of Underscore.js. In addition to porting Underscore's functionality, Underscore.php includes matching unit

Brian Haveri 1.2k Dec 25, 2022
Adds phone number functionality to TYPO3 based on the PHP port of Google's libphonenumber API by giggsey

TYPO3 Phone Adds phone number functionality to TYPO3 based on the PHP port of Google's libphonenumber API by giggsey. Installation composer require si

Simon Schaufelberger 3 Oct 25, 2022
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Jan 2, 2023
⚡ Laravel Charts — Build charts using laravel. The laravel adapter for Chartisan.

What is laravel charts? Charts is a Laravel library used to create Charts using Chartisan. Chartisan does already have a PHP adapter. However, this li

Erik C. Forés 31 Dec 18, 2022
Laravel Kickstart is a Laravel starter configuration that helps you build Laravel websites faster.

Laravel Kickstart What is Laravel Kickstart? Laravel Kickstart is a Laravel starter configuration that helps you build Laravel websites faster. It com

Sam Rapaport 46 Oct 1, 2022
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

null 9 Dec 14, 2022
Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

Laravel Segment Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application. Installation You can install the pac

Octohook 13 May 16, 2022