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.

Download NepaliCalender.php library from here : NepaliCalender

Use the following function to convert English Date (A.D.) to Nepali Date (B.S.)

function getNepaliDate($date){
  $ndate = NealiCalender::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 (B.S.) to English Date (A.D.)

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 all these functions as below to get the date conversion working :

//Include Calendar 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>';

 

Source Code :

 



Recommended For You

About the Author: Admin