PHPackages                             tonysm/local-time-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tonysm/local-time-laravel

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tonysm/local-time-laravel
=========================

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

2.0.0(3mo ago)57900↑13.3%4[1 issues](https://github.com/tonysm/local-time-laravel/issues)MITPHPPHP ^8.2CI passing

Since Jul 1Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/tonysm/local-time-laravel)[ Packagist](https://packagist.org/packages/tonysm/local-time-laravel)[ Docs](https://github.com/tonysm/laravel-local-time)[ RSS](/packages/tonysm-local-time-laravel/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (9)Dependencies (9)Versions (12)Used By (0)

[![Logo Local Time Laravel](/art/local-time-laravel-logo.svg)](/art/local-time-laravel-logo.svg)

 [![](https://github.com/tonysm/local-time-laravel/workflows/run-tests/badge.svg)](https://github.com/tonysm/local-time-laravel/workflows/run-tests/badge.svg) [![Total Downloads](https://camo.githubusercontent.com/d5ade9c08a9d23fa6e8bed12016d36d39b8a273fe0170678a47eb1db5f47b82b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746f6e79736d2f6c6f63616c2d74696d652d6c61726176656c)](https://packagist.org/packages/tonysm/local-time-laravel) [![License](https://camo.githubusercontent.com/d188059e08fe0f40d776b451edcb2d753ca060450f62d197a7cc836216b373bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f746f6e79736d2f6c6f63616c2d74696d652d6c61726176656c)](https://packagist.org/packages/tonysm/local-time-laravel)

This is a Laravel port of the [`local_time`](https://github.com/basecamp/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
------------

[](#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
-----

[](#usage)

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

```

```

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

```

```

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

```

```

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.

```

```

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

[](#time-ago-helper)

```

```

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

[](#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. The default type is `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

[](#example)

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

```

```

Renders:

```
June 30, 2021 22:32pm
```

And is converted client-side to:

```
June 30, 2021 22:32pm
```

### Empty Placeholder

[](#empty-placeholder)

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

```

{{-- Renders " -- " if deleted_at is null --}}
```

You can customize the placeholder globally (see [Configuration](#configuration) below).

### Configuration

[](#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:

```
