Download the PHP package omaralalwi/laravel-time-craft without Composer

On this page you can find all versions of the php package omaralalwi/laravel-time-craft. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-time-craft

Laravel Time Craft

Laravel Time Craft

Latest Version on Packagist Total Downloads GitHub Issues GitHub Stars License

A simple trait and a set of helper functions that let you effortlessly manage date and time queries in Laravel apps — with pre-built Eloquent scopes and formatting helpers, ready to use out of the box.

Table of Contents

Features

Requirements

Each Laravel version keeps its own PHP floor: Laravel 10 needs PHP ^8.1, Laravel 11/12 need ^8.2, and Laravel 13 needs ^8.3. Composer automatically installs the highest Laravel your PHP version supports.

Installation

Install the package via Composer:

The service provider is auto-discovered — no manual registration needed.

(Optional) publish the configuration file:

Configuration

Publishing creates config/laravel-time-craft.php:

When a scope needs to know which column to filter on, it resolves the field through a three-level fallback (first match wins):

  1. The field passed directly to the scope call.
  2. The model's protected $dateField property.
  3. The default_field value from the config (created_at by default).

See Customizing the Date Field for examples.

Usage

The HasDateTimeScopes Trait

Add the trait to any Eloquent model:

Available Scopes

Heads up: the *Ago scopes (oneWeekAgo, oneMonthAgo, oneYearAgo) match a single, exact day in the past — not a range. To filter everything since a point in time, use the lastNDays range scopes instead.

Day

Scope Description Example
today() Records dated today. Order::today()->get();
yesterday() Records dated yesterday. Order::yesterday()->get();

Exact day in the past (*Ago)

Scope Description Example
oneWeekAgo() Records dated exactly 7 days ago. Order::oneWeekAgo()->get();
oneMonthAgo() Records dated exactly 30 days ago. Order::oneMonthAgo()->get();
oneYearAgo() Records dated exactly one year ago. Order::oneYearAgo()->get();

Last N days (range, from N days ago until now)

Scope Description Example
last7Days() Records from the last 7 days. Order::last7Days()->get();
last10Days() Records from the last 10 days. Order::last10Days()->get();
last14Days() Records from the last 14 days. Order::last14Days()->get();
last15Days() Records from the last 15 days. Order::last15Days()->get();
last21Days() Records from the last 21 days. Order::last21Days()->get();
last30Days() Records from the last 30 days. Order::last30Days()->get();
lastDays($field = null, $days = 7) Records from the last $days days. Order::lastDays(null, 12)->get();

Argument order for lastDays: the field comes first, the number of days second. Pass null for the field to use the default. Example: Order::lastDays(null, 5)->get(); filters the last 5 days on the default field; Order::lastDays('updated_at', 5)->get(); filters on updated_at.

Week

Scope Description Example
currentWeek() Records in the current calendar week. Order::currentWeek()->get();
lastWeek() Records in the previous calendar week. Order::lastWeek()->get();

Month

Scope Description Example
currentMonth() Records in the current month. Order::currentMonth()->get();
lastMonth() Records in the previous month. Order::lastMonth()->get();

Year

Scope Description Example
currentYear() Records in the current year. Order::currentYear()->get();
lastYear() Records in the previous year. Order::lastYear()->get();

Custom range

Scope Description Example
betweenDates($start, $end, $field = null) Records within an inclusive date range. Accepts Carbon instances or Y-m-d strings. Order::betweenDates('2024-01-01', '2024-01-31')->get();

Customizing the Date Field

All scopes use created_at by default. You can override the field in three ways:

1. Globally, for every model, via the config file:

2. Per model, by adding a $dateField property:

3. Per call, by passing the field name directly:

A Note on Timezones

Scopes are built on Laravel's now() / today() helpers, so "today", "this week", etc. are evaluated against your application timezone (config('app.timezone')). Make sure your app timezone and the stored timestamps are consistent to get the results you expect.

The HasReadableDates Trait

Tired of formatting created_at / updated_at / deleted_at in every model, controller, and Blade view? Add the trait once and get ready-to-display, human-readable date accessors:

Non-destructive by design: unlike overriding the timestamp attributes directly, this trait adds separate readable_* accessors. Your real $order->created_at keeps returning a Carbon instance, so date math, sorting, comparisons and casting all keep working — you just get a formatted string on the side.

Format any date field. Use readableDate() for columns beyond the standard timestamps:

Include them in JSON / API responses by appending the accessors:

Safe on models without the columns. readable_deleted_at returns null unless the model uses SoftDeletes (detected recursively, so it also works when SoftDeletes comes from a parent class), and the readable_created_at / readable_updated_at accessors return null when the model has public $timestamps = false.

Customizing the format

The format is resolved through a three-level fallback (first match wins):

  1. The model's protected $readableDateFormat property.
  2. The readable_datetime_format value from the config.
  3. The built-in default, F j, Y g:i A.

Helper Functions

These globally-available functions can be used anywhere (Blade files, classes, controllers). Each accepts a \DateTime/Carbon instance or a date string.

Function Description Example Output
formatDate($date) Format a date as Y-m-d. formatDate($order->created_at) 2024-08-25
formatTime($time) Format a time as h:i:s A. formatTime($order->created_at) 10:38:12 PM
formatDateTime($dateTime) Format date + time as Y-m-d H:i:s A. Parses strings too. formatDateTime($order->created_at) 2017-02-15 10:38:12 PM
getHumanDateTime($createdAt) Format a Carbon instance as Y-m-d H:i:s A. Returns the input unchanged if it is not a Carbon instance. getHumanDateTime($order->created_at) 2017-02-15 10:38:12 PM
formatTimeAgo($dateTime) Human-readable "time ago". formatTimeAgo($order->created_at) 2 days ago
startOfDay($date) Start of the given day. startOfDay($order->created_at) 2024-08-23 00:00:00
endOfDay($date) End of the given day. endOfDay($order->created_at) 2024-08-23 23:59:59
isWeekend($date) Whether the date falls on Sat/Sun. isWeekend($order->created_at) true / false
addDays($date, $days) Add days, returns Y-m-d. addDays($order->created_at, 10) 2024-09-02
subtractDays($date, $days) Subtract days, returns Y-m-d. subtractDays($order->created_at, 10) 2024-08-13

getHumanDateTime and formatDateTime produce the same format, but differ in input handling: getHumanDateTime only formats Carbon instances (anything else is returned as-is), while formatDateTime also parses date strings.

Testing

The package uses Orchestra Testbench to boot a minimal Laravel app for testing.

Changelog

Please see CHANGELOG for details on what has changed recently.

Contributing

Contributions are welcome! Please read our contributing guidelines before submitting a pull request.

License

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


📚 More Open Source Packages


All versions of laravel-time-craft with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1|^8.2|^8.3|^8.4
illuminate/support Version ^10.0|^11.0|^12.0|^13.0
illuminate/database Version ^10.0|^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package omaralalwi/laravel-time-craft contains the following files

Loading the files please wait ...