Verta PHP Package

repository·master·Indexed 20 days ago

https://github.com/hekmatinasser/verta

A PHP package for bridging Gregorian and Jalali (Solar Hijri) calendars. It extends native PHP Datetime and Jalali classes and provides seamless compatibility with the Carbon library. Verta includes features for date conversion, formatting, arithmetic, and localization, as well as a Laravel facade and custom validation rules for Jalali dates and datetimes.

Tokens
3K
Snippets
14
Records
16
Agent score
71%

What's inside Verta

  1. Install Verta via Composer

    master

    To use Verta in your PHP project, install it using Composer. Verta is compatible with various Laravel versions as follows:

    Laravel VersionVerta Version
    8.08.0
    9.08.2
    10.08.3
    11.08.4
    12.08.5
    composer require hekmatinasser/verta
  2. Convert between Gregorian and Jalali calendars

    master

    Verta allows seamless conversion between the Gregorian and Jalali (Solar Hijri) calendars.

    Gregorian to Jalali

    Instantiate Verta with a Gregorian date string to get a Jalali instance.

    Jalali to Gregorian

    Use Verta::parse() with a Jalali date string, then call ->datetime() to retrieve the Gregorian equivalent.

    Carbon to Jalali

    If you are using the Carbon library, you can call ->toJalali() on a Carbon instance.

    Jalali to Carbon

    Call ->toCarbon() on a Verta instance to convert it back to a Carbon instance.

    // Gregorian to Jalali
    echo verta('2022-08-15'); // 1401-05-24 00:00:00
    
    // Jalali to Gregorian
    echo Verta::parse('1401-05-24 14:12:32')->datetime(); // 2022-08-15 00:00:00
    
    // Carbon to Jalali
    echo now()->toJalali(); // 1401-05-24 00:00:00
    
    // Jalali to Carbon
    echo verta()->toCarbon(); // 2022-08-15 00:00:00
  3. Customize Jalali validation error messages

    master
    When using comparison rules like dateAfter, dateBefore, or their Equal variants, you can use the :date placeholder in your Laravel validation messages to inject the reference date. The validator automatically handles locale-specific number formatting (e.g., converting English digits to Persian digits if the locale is not 'en').
  4. Manipulate and compare Jalali dates

    master

    Verta provides tools for date arithmetic, boundary setting, and comparison.

    Modification

    Use methods like addWeeks() to manipulate the date.

    Boundaries

    Use methods like startWeek() to jump to the beginning of a specific period.

    Comparison (Compression)

    Use comparison methods like gte() (greater than or equal) to compare the current instance against another date.

    Difference

    Calculate the difference between two dates using methods like diffMonths().

    // Modification
    echo verta()->addWeeks(3);
    
    // Boundaries
    echo verta()->startWeek(3);
    
    // Comparison
    echo verta('+2 day')->gte('2022-08-15');
    
    // Difference
    echo verta('+13 day')->diffMonths('2022-08-15');
  5. Format Jalali dates

    master

    Verta supports standard date formatting and specialized Jalali formatting.

    Standard Formatting

    Use format() with standard PHP date characters (e.g., Y.m.d).

    Word Formatting

    Use formatWord() to output the date in written words (localized).

    Common Jalali Formats

    Use formatJalaliDatetime() for a quick, standard Jalali datetime string (e.g., 1395/10/07 14:12:25).

    echo verta()->format('Y.m.d'); // 1401.05.24
    echo verta()->formatWord('l dS F'); // دوشنبه بیست و چهارم مرداد
    echo verta()->formatJalaliDatetime(); // 1395/10/07 14:12:25
  6. Validate Jalali dates and Laravel requests

    master

    Static Validations

    Use Verta::isLeapYear() to check if a specific Jalali year is a leap year.

    Laravel Validation

    In Laravel validation rules, you can use Verta-specific rules such as jdate_before_equal to validate Jalali date inputs in request objects.

    // Check leap year
    echo Verta::isLeapYear(1394); // false
    
    // Laravel validation rule example
    'birthday' => ['required', 'jdate_before_equal']
  7. Access and set Jalali date components

    master

    Verta provides getters and setters to interact with specific parts of a Jalali date (e.g., year, month, day).

    Getters

    Access components directly as properties (e.g., $v->year).

    Setters

    Assign values directly to properties to modify the date.

    Fluent Setters

    Use methods like setTimeString() to set multiple time components at once and return the instance for chaining.

    // Getters
    $v = verta();
    echo $v->year; // 1396
    
    // Setters
    $v->year = 1395;
    
    // Fluent Setters
    $v->setTimeString('12:25:45');
  8. Human-readable differences and Localization

    master

    Difference for Humans

    Use formatDifference() to get a localized, human-readable string representing the time difference (e.g., "1 year ago").

    Localization

    Set the language for all localized outputs (like formatWord or formatDifference) using Verta::setLocale().

    // Human readable difference
    echo verta('-13 month')->formatDifference(); // 1 سال قبل
    
    // Set locale
    Verta::setLocale('ar');
  9. Convert a Verta instance to a Carbon instance

    master

    Use the toCarbon() method to convert a Verta object into an Illuminate\Support\Carbon instance. This is useful when you need to integrate Jalali date manipulation with Laravel's Carbon-based ecosystem or use Carbon's specific date/time methods. The resulting Carbon instance will respect the timezone of the original Verta instance.

    $verta = new Verta('1402/01/01');
    $carbon = $verta->toCarbon();
  10. Validate Jalali dates in Laravel

    master

    Use the validateDate rule to check if a string is a valid Jalali date. By default, it expects the format Y/m/d. You can provide a custom format as the first parameter.

    Parameters:

    • [0]: (Optional) The expected date format (e.g., Y/m/d).
    // Example Laravel validation rule usage
    $request->validate([
        'birthday' => 'date:Y/m/d',
    ]);
  11. Compare Jalali dates and datetimes in Laravel

    master

    The JalaliValidator provides several rules for comparing a Jalali date or datetime against a reference value.

    Date Comparison Rules

    • date:format: Validates format (default Y/m/d).
    • dateEqual:reference_date,format: Must be equal to reference_date.
    • dateNotEqual:reference_date,format: Must not be equal to reference_date.
    • dateAfter:reference_date,format: Must be strictly after reference_date.
    • dateAfterEqual:reference_date,format: Must be after or equal to reference_date.
    • dateBefore:reference_date,format: Must be strictly before reference_date.
    • dateBeforeEqual:reference_date,format: Must be before or equal to reference_date.

    DateTime Comparison Rules

    • datetime:format: Validates format (default Y/m/d H:i:s).
    • datetimeEqual:reference_datetime,format: Must be equal to reference_datetime.
    • datetimeNotEqual:reference_datetime,format: Must not be equal to reference_datetime.
    • datetimeAfter:reference_datetime,format: Must be strictly after reference_datetime.
    • datetimeAfterEqual:reference_datetime,format: Must be after or equal to reference_datetime.
    • datetimeBefore:reference_datetime,format: Must be strictly before reference_datetime.
    • datetimeBeforeEqual:reference_datetime,format: Must be before or equal to reference_datetime.

    Parameter Mapping:

    • parameters[0]: The reference date/datetime string.
    • parameters[1]: The format to use for both the reference and the input value.