一个将 DateTime 格式化为孟加拉语并支持孟加拉语日历的 Laravel 扩展包。

1.0.1 2024-09-13 11:33 UTC

This package is auto-updated.

Last update: 2024-09-13 22:57:27 UTC


README

BanglaDateTime 是一个 Laravel 扩展包,允许您轻松地将日期时间格式化为孟加拉语日历,并转换为孟加拉数字格式。它支持当前时间和自定义日期,以及时区。

功能

  • 将 DateTime 转换为孟加拉语格式。
  • 转换为孟加拉语日历(孟加拉历)。
  • 支持自定义日期和时区。
  • 使用流畅的 API 易于使用。
  • 提供辅助函数以方便访问常见操作。

安装

您可以通过 Composer 安装此包

composer require bangladatetime/laravel

Laravel 集成

要集成 BanglaDateTime,请按照以下步骤操作

  1. 发布配置文件

    运行以下命令以发布包配置文件

    php artisan vendor:publish --provider="BanglaDateTime\Laravel\BanglaDateTimeServiceProvider"

    这将在 config/bangladatetime.php 创建一个配置文件。

  2. 配置

    编辑 config/bangladatetime.php 文件以设置所需的日期格式和时区。

  3. 使用方法

    该包与 Laravel 的 Carbon 类集成,允许您使用自定义方法直接在孟加拉语格式和孟加拉语日历中格式化日期。

与 Eloquent 模型一起使用

如果您有一个具有 datetime 转换的模型,您可以使用 BanglaDateTime 方法轻松地格式化日期时间属性。例如

use App\Models\User;

// Retrieve a user and format the created_at attribute in Bangla format
$user = User::find(1);
echo $user->created_at->formatBangla('l jS F Y h:i:s');

// Format the created_at attribute in Bangla calendar format
echo $user->created_at->toBangla('l jS F Y h:i:s');

输出

Created at with Bangla format: বৃহস্পতিবার ১২ই সেপ্টেম্বর ২০২৪ ০৩:৩৭:০৩
Created at converted to Bangla: বৃহস্পতিবার ২৮শে ভাদ্র ১৪৩১ ০৩:৩৭:০৩

基本示例

use Illuminate\Support\Carbon;

// Get current date and time in Bangla format
echo Carbon::now()->formatBangla('l jS F Y h:i:s');

// Get current date and time in Bangla calendar format
echo Carbon::now()->toBangla('l jS F Y h:i:s');

输出

Current time with Bangla format: বৃহস্পতিবার ১২ই সেপ্টেম্বর ২০২৪ ০৩:৩৭:০৩
Current time converted to Bangla: বৃহস্পতিবার ২৮শে ভাদ্র ১৪৩১ ০৩:৩৭:০৩

设置自定义日期

use Illuminate\Support\Carbon;

// Set a custom date ('2023-04-13') in Bangla format
echo Carbon::create('2023-04-13')->formatBangla('l jS F Y h:i:s');

// Set a custom date ('2023-04-13') in Bangla calendar format
echo Carbon::create('2023-04-13')->toBangla('l jS F Y h:i:s');

输出

Set Time with Bangla format: বৃহস্পতিবার ১৩ই এপ্রিল ২০২৩ ১২:০০:০০
Set Time & converted to Bangla: বৃহস্পতিবার ৩০শে চৈত্র ১৪২৯ ১২:০০:০০

处理时区

use Illuminate\Support\Carbon;

// Set current time with a specific timezone ('Asia/Dhaka') in Bangla format
echo Carbon::now('Asia/Dhaka')->formatBangla('l jS F Y h:i:s');

// Set current time with a specific timezone ('Asia/Dhaka') in Bangla calendar format
echo Carbon::now('Asia/Dhaka')->toBangla('l jS F Y h:i:s');

输出

Set Time & Time Zone with Bangla format: বৃহস্পতিবার ১২ই সেপ্টেম্বর ২০২৪ ০৯:৩৭:০৩
Set Time & Time Zone & converted to Bangla: বৃহস্পতিবার ২৮শে ভাদ্র ১৪৩১ ০৯:৩৭:০৩

自定义格式和时区

在调用 formatBanglatoBangla 方法时,您可以指定自定义格式和时区

use Illuminate\Support\Carbon;

// Get current date and time in Bangla format with a custom format and timezone
echo Carbon::now()->formatBangla('d/m/Y H:i:s', 'Asia/Dhaka');

// Get current date and time in Bangla calendar format with a custom format and timezone
echo Carbon::now()->toBangla('d/m/Y H:i:s', 'Asia/Dhaka');

输出

Custom Time with Bangla format: ১২/০৯/২০২৪ ১৫:৩৭:০৩
Custom Time & converted to Bangla: ২৮/ভাদ্র/১৪৩১ ১৫:৩৭:০৩

API

BanglaDateTime::create($time = 'now', $timezone = 'UTC')

创建一个新的 BanglaDateTime 实例。

  • $time: 要使用的日期和时间(可选,默认为 'now')。
  • $timezone: 要使用的时区(可选,默认为 'UTC')。

format($format)

使用指定的格式格式化日期和时间,输出为孟加拉语区域。

  • $format: 格式化字符串(与 PHP 的 DateTime::format 相同)。

toBangla($format)

将日期和时间转换为孟加拉语日历和孟加拉数字格式。

  • $format: 格式化字符串(与 PHP 的 DateTime::format 相同)。

辅助函数

BanglaDateTime 提供以下全局辅助函数以方便访问

  • bangla_date_time($time = 'now', $timezone = 'UTC'): 创建一个新的 BanglaDateTime 实例。

    $date = bangla_date_time('2023-04-13', 'Asia/Dhaka');
  • format_bangla_date($format, $time = 'now', $timezone = 'UTC'): 格式化日期/时间为孟加拉语格式。

    echo format_bangla_date('l jS F Y h:i:s');
  • convert_to_bangla_calendar($format, $time = 'now', $timezone = 'UTC'): 将日期/时间转换为孟加拉语日历格式。

    echo convert_to_bangla_calendar('l jS F Y h:i:s');

贡献

请随时通过提交拉取请求或打开问题来贡献。您的贡献非常受赞赏!

许可证

此库是开源软件,采用 MIT 许可证