PHP code example of dipesh / nepali-date

1. Go to this page and download the library: Download dipesh/nepali-date library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dipesh / nepali-date example snippets


use Dipesh\NepaliDate\NepaliDate;

$date = new NepaliDate(language: 'np or en'); // Creates current date instance with provided language.

//or
$date = $date->setLang('np') or $date->setLang(new \Dipesh\NepaliDate\lang\Nepali()) // Creates immutable instance

//or
$date = new NepaliDate("2050-8-10") // Creates date instance with default language configuration
//You are free to use either of these formats, e.g., yyyy-mm-dd or yyyy/mm/dd." 
//or
$date = NepaliDate::make("2070-8-20"); // Creates date instance with default language configuration

//or
$date = NepaliDate::now(); // Creates current date instance

// Work with global instance
$date->create($date); // Creates an immutable date instance while retaining the previous configuration settings.


$date->toAd(); // creates php Date instance
//or
$date = NepaliDate::fromADDate("1990-9-10");

$date->year();   // Retrieves the year
$date->month($format);  // Retrieves the formatted month
$date->day();    // Retrieves the day
$date->weekDay($format); // Retrieves the formatted week day

$date->addDays($days);

$date->subDays($days);

$date->isEqual('2048/10/5'); // return true or false

$date->isGreaterThan('2048/10/5');

$date->isLessThan('2048/10/5');


$date->format('Y-m-d'); // 2050-10-8

$date->format('Y F d g l'); // 2050 Magh 8 Gate Sukrabar

$date->format("Y-m-d, M d g l") // २०५०-१०-२८, माघ २८ गते बिहिबार"

//or
$date->format("Y-m-d, M d g l", 'np')

// Extending the main NepaliDate class
class CustomDate extends \Dipesh\NepaliDate\NepaliDate
{
    // Your new feature implementation goes here

    // Use a custom date processor for all date-related logic
    public function getDateProcessor()
    {
        return new CustomDateProcessor();
    }

    // Use a custom formatter for all date formatting
    public function getFormatter()
    {
        return new CustomFormatter();
    }
}

// Implementing a custom DateProcessor
class CustomDateProcessor implements \Dipesh\NepaliDate\Contracts\DateProcessor
{
    public function getDays(int $year, int $month, int $day): int
    {
        // Your custom logic for calculating days
    }

    public function getDateFromDays(int $totalDays): string
    {
        // Your custom logic for calculating a date from total days
    }

    public function getWeekDayFromDays(int $days): int
    {
        // Your custom logic for determining the weekday from days
    }
}

// Implementing a custom Formatter
class CustomFormatter implements \Dipesh\NepaliDate\Contracts\Formatter
{
    public function setUp(Date $date): static
    {
        // Setup logic with the date
    }

    public function format(string $format): string
    {
        // Your custom logic for formatting the date
    }

    public function formatNumber(int $number): string
    {
        // Your custom logic for formatting numbers
    }

    public function formatMonth(string $format = 'm'): mixed
    {
        // Your custom logic for formatting months
    }

    public function formatWeekDay(string $format = 'w'): mixed
    {
        // Your custom logic for formatting weekdays
    }
}

// Implementing a custom language
class CustomLanguage implements \Dipesh\NepaliDate\Contracts\Language
{
    public function getGate(): string
    {
        // Your custom language specific gate especially useful for nepali language and you might not need this
        // Eg: return "";
    }

    public function getDigit(int $digit):int|string
    {
        // Your custom language specific digit
        // Eg: return 1;
    }

    public function getWeek(int $week):array
    {
        // Your custom language specific week day
        // Eg: return ['l' => 'Sunday', 'D' => 'Sun'];
    }

    public function getMonth(int $month):array
    {
        // Your custom language specific month
        // Eg: return ['F' => 'January', 'M' => 'Jan'];
    }
}