BS / AD Date Converter using PHP

In this tutorial, we are going to use a PHP library NepaliCalender.php to convert date from AD to BS and BS to AD.

Follow this video for complete guidance :

Download NepaliCalendar.php library from here : NepaliCalender

Use the following function to convert English Date (AD) to Nepali Date (BS)

function getNepaliDate($date){
    $ndate = NepaliCalender::getInstance()->eng_to_nep($date);
    $ndate = $ndate['nmonth_in_nepali'].' '.$ndate['date_in_nepali'].', '.getNepaliNumber($ndate['year']);
    return $ndate;
} 

getNepaliNumber() function is used to convert digit to Nepali text.

function getNepaliNumber($str) {
    $find = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
    $replace = array("०", "१", "२", "३", "४", "५", "६", "७", "८", "९");
    return str_replace($find, $replace, $str);
}

Use the following function to convert Nepali Date (BS) to English Date (AD)

function getEnglishDate($date){
    $year = date('Y',strtotime($date));
    $month = date('m',strtotime($date));
    $day = date('d',strtotime($date));
    $edate = NepaliCalender::getInstance()->nep_to_eng($year,$month,$day);
    $date = $edate['year'].'-'.$edate['month'].'-'.$edate['date'];
    return $date;
}

Now we can call these functions as below to get the date conversion working :

//incude Calender Library
include('NepaliCalender.php');

//AD to BS Conversion
$english_date = date('Y-m-d');
$nepali_date = getNepaliDate($english_date);
echo '<h2>'.$english_date.' AD into '.$nepali_date.' BS</h2>';

//BS to AD Conversion
$nepali_date = '2078-05-26';
$english_date = getEnglishDate($nepali_date);
echo '<h2>'.$nepali_date.' BS into '.$english_date.' AD</h2>';

 

Recommended For You

About the Author: Ritesh Ghimire

Ritesh is a Computer Engineer, Web Developer and an active cricket follower.