omaralalwi/laravel-time-craft 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

omaralalwi/laravel-time-craft

Composer 安装命令:

composer require omaralalwi/laravel-time-craft

包简介

Laravel Time Craft is a powerful package for handling date and time scopes in your Laravel applications. It provides a set of convenient traits and helper functions to simplify date and time operations.

README 文档

README

Laravel Time Craft

Latest Version on Packagist Total Downloads GitHub Issues GitHub Stars License

simple trait and helper functions that allow you, Effortlessly manage date and time queries in Laravel apps, with pre-built scopes and helper functions with ease.

Features

  • Flexible Date Scopes: Easily filter records based on various time frames (e.g., today, yesterday, current week, last month, etc.).
  • Helper Functions: Utility functions for formatting dates, times, and human-readable date-time representations.
  • Dynamic Field Support: Scopes can be applied to any date or time field in your models.

Installation

You can install the package via Composer:

composer require omaralalwi/laravel-time-craft

publish the package's configuration file:

php artisan vendor:publish --tag=laravel-time-craft

Usage

Using the HasDateTimeScopes Trait

Add the HasDateTimeScopes trait to your Eloquent model:

use Omaralalwi\LaravelTimeCraft\Traits\HasDateTimeScopes;

class Order extends Model
{
    use HasDateTimeScopes;
}

Available Scopes in HasDateTimeScopes trait

You can apply various scopes in your model queries. Below are the descriptions, usage examples, and corresponding outputs for each scope:

  • today : Filters records created today.

    $todayOrders = Order::today()->get();
  • yesterday : Filters records created yesterday.

    $yesterdayOrders = Order::yesterday()->get();
  • oneWeekAgo : Filters records created in the last seven days.

    $lastSevenDaysOrders = Order::oneWeekAgo()->get();
  • lastWeek : Filters records created in the last week.

    $lastWeekOrders = Order::lastWeek()->get();
  • currentWeek : Filters records created in the current week.

    $currentWeekOrders = Order::currentWeek()->get();
  • last7Days : Filters records created in the last 7 days.

    $last7DaysOrders = Order::last7Days()->get();
  • last10Days : Filters records created in the last 10 days.

    $last10DaysOrders = Order::last10Days()->get();
  • last14Days : Filters records created in the last 14 days.

    $last14DaysOrders = Order::last14Days()->get();
  • last15Days : Filters records created in the last 15 days.

    $last15DaysOrders = Order::last15Days()->get();
  • last21Days : Filters records created in the last 21 days.

    $last21DaysOrders = Order::last21Days()->get();
  • last30Days : Filters records created in the last 30 days.

    $last30DaysOrders = Order::last30Days()->get();
  • lastDays($days) : Filters records created in the last number of days specified.

    // Filters records created in the last 5 days
    $last5DaysOrders = Order::lastDays(null,5)->get(); // null mean take default field 'created_at' , or you can pass it 'created_at'
    // Filters records created in the last 12 days
    $last10DaysOrders = Order::lastDays(null,12)->get();
  • oneMonthAgo : Filters records created in the last 30 days.

    $ordersLast30Days = Order::oneMonthAgo()->get();
  • lastMonth : Filters records created last month.

    $lastMonthOrders = Order::lastMonth()->get();
  • currentMonth : Filters records created in the current month.

    $thisMonthOrders = Order::currentMonth()->get();
  • lastYear : Filters records created in the last year.

    $lastYearOrders = Order::lastYear()->get();
  • oneYearAgo : Filters records created exactly one year ago.

    $oneYearAgoOrders = Order::oneYearAgo()->get();
  • currentYear : Filters records created in the current year.

    $thisYearOrders = Order::currentYear()->get();
  • betweenDates : Filters records within a specific date range.

    $ordersBetweenDates = Order::betweenDates('2024-01-01', '2024-01-31')->get();

Customize Scoping field

all scopes using created_at by default. You can override by three ways:-

in config file as default for all models

'default_field' => 'your_specific_field'

customize it for every model : by adding following line in model class:

class Order extends Model
{
    use HasDateTimeScopes;

    protected $dateField = 'updated_at';
}

pass field name directly when using the scopes:

$lastWeekOrders = Order::lastWeek('updated_at')->get();

Format Date Time Using Helper Functions

You can use the provided helper functions in your application (in Blade files or in any class). Below are the descriptions, usage examples, and corresponding outputs for each helper function:

  • formatDate : Formats a given date to "Y-m-d" format.

    $formattedDate = formatDate($order->created_at); // 2024-08-25
  • formatTime : Formats a given time to "h:i:s A" format.

    $formattedTime = formatTime($order->created_at); // 10:38:12 PM
  • getHumanDateTime : Formats the created_at datetime to "Y-m-d H:i:s A" format.

    $humanDateTime = getHumanDateTime($order->created_at); // 2017-02-15 10:38:12 PM
  • formatDateTime : Formats a given date and time to "Y-m-d H:i:s A" format.

    $formattedDateTime = formatDateTime($order->created_at); // 2017-02-15 10:38:12 PM
  • formatTimeAgo : Gets a human-readable "time ago" format for a given date.

    $timeAgo = formatTimeAgo($order->created_at); // 2 days ago
  • startOfDay : Gets the start of the day for a given date.

    $startOfDay = startOfDay($order->created_at); // 2024-08-23 00:00:00
  • endOfDay : Gets the end of the day for a given date.

    $endOfDay = endOfDay($order->created_at); // 2024-08-23 23:59:59
  • isWeekend : Checks if a given date is on the weekend.

    $isWeekend = isWeekend($order->created_at); // true or false
  • addDays : Adds a specified number of days to a given date.

    $futureDate = addDays($order->created_at, 10); // 2024-09-02
  • subtractDays : Subtracts a specified number of days from a given date.

    $pastDate = subtractDays($order->created_at, 10); // 2024-08-13

Helpful Packages

You may be interested in our other packages:

  • Gpdf : PDF converter for php & Laravel apps, support storing PDF files to S3.
  • laravel-taxify : simplify tax (VAT) calculations in laravel apps.
  • laravel-deployer : Streamlined deployment for Laravel and Node.js apps.
  • laravel-trash-cleaner : Cleans logs and debug files for logs and debugging packages.

Tests

tests will coming soon.

Contributing

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

License

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

📚 Helpful Open Source Packages

  • lexi translate Lexi Translate simplify managing translations for multilingual Eloquent models with power of morph relationships and caching .

  • laravel Taxify Gpdf Open Source HTML to PDF converter for PHP & Laravel Applications, supports Arabic content out-of-the-box and other languages..

  • laravel Taxify laravel Taxify Laravel Taxify provides a set of helper functions and classes to simplify tax (VAT) calculations within Laravel applications.

  • laravel Deployer laravel Deployer Streamlined Deployment for Laravel and Node.js apps, with Zero-Downtime and various environments and branches.

  • laravel Trash Cleaner laravel Trash Cleaner clean logs and debug files for debugging packages.

  • Laravel Startkit Laravel Startkit Laravel Admin Dashboard, Admin Template with Frontend Template, for scalable Laravel projects.

omaralalwi/laravel-time-craft 适用场景与选型建议

omaralalwi/laravel-time-craft 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.14k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2024 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.14k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-24