2018-11-09 Pujan Niroula 2 Minute(s) Read

Nepali Date [AD-BS] Converter in PHP

img-open source ad bs.png

If someone says I celebrate my birthday 3 times in a year then he must be lying or he must be Nepalicool. First birthday according to Bikram Sambat date, Second according to After Christ (AD) and third one according to tithi (Lunar Date) system. In this post I will discuss just about two date system and share Free AD-BS Converter in PHP. Because of government and society we must use BS date system and for internet we must use AD date system. Let's make it easy with this wonderful AD/BS converter.

Developing Nepali Date Converter Script

As I mentioned on my first post during daily new idea development period I thought to add multi date system in this So Called CMS. I googled and found desired converter but that has date limitation. If my grandfather want to convert his birthday then it will shows error. So I decided to write my own Nepali date converter in PHP for this blog. You can change date system and language of this blog from Site Setting.

It's completely based on arrays and date system of PHP. I have collected data from different sources. It's little boring to collect the correct data and use those data. I wrote it in very simple way based on counting days between dates. I thought it as most effective idea.

How to Use Nepali Date[ad/bs] Converter on Your Website?

Simply download and upload it to your server. Include it where you want to use. For eg. if you have to display it on index file and include it there and call for action... See my example below. If you want to display time too then use echo $date['l']; (Small L). and you can echo month name or month number by changing Case of $date['m']. Simply I can say you can use pretty same as date() function in PHP, 'm' means month number, 'M' mean month name etc...

How to store time on database for this converter?

It's simple just store time(). This function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

How I Used This PHP Script?

I used it in very simple way. here is my code

$y = date('Y', time()); //Getting Year
$m = date('m', time()); //Getting Month
$d = date('d', time()); //Getting Day
require('nepali-date.php'); //Including library
$date = new nepali_date;
$date = $date->get_nepali_date($y, $m, $d);
echo $date['y'].'-'.$date['m'].'-'.$date['d']; //Printing Date

Lets describe it.
First I got current AD date (Year, Month and day). and called the class and this converter did rest and finally I print all dates.

Wishing Birthday on Nepali[B.S] Dates

If you own sites which is build for Nepali People then you want to wish them on Nepali Date but PHP doesn't support Bikram Sambat date system. Here you can use this to wish them. Store Nepali Date on database and Check today's date with this script. If month and date matched wish them. I have attached full code along with the script.

 Naming This

I almost forgot to name this item.laughing According to vedic astrology a famous astrologer suggested its name - Nepali Date Converter. tongue-out Further he added this name will be beneficial for SEO purpose too. Some astrologers suggested AD/BS date converter but According to market I thought the first one will be great.

Example

You can Check this free Online Nepali Date Converter based on this script.

 Thanks for reading... Glad to see you in comment section below.

Download Attached Files: Nepali Date Converter [Open Source].zip
Comments (100) Add New Comment
Umesh Gaire2024-06-03 5:16 AM

Please help me to generate nepali date like this: 2081-02-21 in AD we can do with date('Y-m-d'); I hope you understood what I am expecting.

Reply
Ramesh2023-08-21 11:25 AM

Invalid Year range, when selected day 32 of the month

Reply
Laxman Adhikari2023-04-10 2:57 PM

hello i what to know how array of months is calculated .

Reply
Njn Khanal2022-11-19 7:51 AM

At first thank you so much for this, everything working well but when converting to nepali date month end like kartik 31 its showing mansir 1

Reply
Muniraj Rajbanshi2021-05-13 7:56 AM

How to get two digit year ?

Reply
Pujan Niroula2021-05-30 6:07 PM

No option, you can trim first two digit :) get your hands dirty

Sujan Stc2021-04-14 2:29 PM

If you are facing problem tallying nepali date and english date with 'Asia/Kathmandu' timezone, use timezone_open('UTC') on every date_create function like so: $date_start=date_create($this->firstday_en,timezone_open('UTC')); This will perfectly tally with english time and you dont have to change to UTC if your site is already using 'Asia/Kathmandu' timezone.

Reply
Pujan Niroula2021-05-30 6:08 PM

Thanks :)

Gopal Nepal2020-09-18 11:05 AM

Hello sir, I want to display format like this २०७७ असोज २ . But, find format for month not for year and day. How to do that ?

Reply
Pujan Niroula2020-09-29 2:59 AM

You can convert output date to devanagari Numbers with this function <?php function devanagari($num) { $num_nepali = array('०','१','२','३','४','५','६','७','८','९'); $num_eng = array('0','1','2','3','4','5','6','7','8','9'); $nums = str_replace($num_eng, $num_nepali, $num); return $nums; } echo devanagari('13905'); //Output १३९०५ ?>

John Doe2020-06-28 3:46 AM

Does anybody has ad-bs converter in javascript

Reply
Hiramani Pun2020-06-03 10:20 AM

today is 2020/06/03 and in nepali 2077/02/21, its showing nepali date 2077/2/20 , what's the problem ?

Reply
Sujan Stc2021-04-14 2:31 PM

Simple solution is above at my comment. Please check.

Pujan Niroula2020-06-03 1:06 PM

Set timezones to UTC. Place date_default_timezone_set('UTC'); before require("nepali-date.php");

Ankit Subedi2020-01-16 5:06 AM

First of all, thank you so much for the converter. But, I have a problem. The code works perfectly fine on localhost, but shows "Fatal error: Uncaught Error: Call to undefined function cal_days_in_month()" this error when deployed. Is there anything i am missing?

Reply
Pujan Niroula2020-01-17 4:54 PM

Sorry! I can't do anything for that. Looks like server side problem. :(

Yogendra2019-12-05 5:49 PM

I have been trying to use your code but it is returning the non numeric error and couldnot use it.could you please help out

Reply
Pujan Niroula2019-12-10 9:06 AM

I have updated the code. Please check, thanks :)

InNepall2019-11-06 4:14 AM

Can you please update the code. I am getting an error with PHP 7.3 "Warning: A non-numeric value encountered in ../nepali-date.php on line 190" Thank you for sharing your hard work.

Reply
Pujan Niroula2019-12-10 9:11 AM

I have updated the code, Please check thanks for your patience. :) Anyway there was little problem on 186 line. Compare to find out more :-D

Binod Stha2018-11-13 5:22 AM

Hi, Thanks for sharing. Please share your nepali date converter's full code also.

Reply
Baha2019-09-10 4:03 AM

https://github.com/bahadurbaniya/Date-Converter-Bikram-Sambat-to-English-Date full code in Java here.

Pujan Niroula2018-12-13 8:44 AM

Sorry for late reply... Currently we don't have plan to share these scripts, In future we will... Hope you understand...