How to print Nepali date today in PHP?

Suman KC picture Suman KC · Feb 4, 2014 · Viewed 8.2k times · Source

i want to echo print nepali calendar date on different date formats. please take a look at bellow code

And there is a jquery date picker for this: http://sajanmaharjan.com.np/my-works/nepali-datepicker-ui/

Now, all i want is to print 2070-10-21 as today date. (like (date(y-m-d) returns as English date)

What i have tried is this:

 <div id="time">

        <!-- PRINTING NEPALI DATE AND TIME -->

        <span class="time_date">
        <?php 
        $timezone = "Asia/Kathmandu";
        if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
        echo date('l F j, Y');?>  &nbsp;| &nbsp; <script type='text/javascript'>var __ndq = __ndq || {format:'W, M D, Y',color:'#555555'};var __sn = document.getElementsByTagName('script'); __sn = __sn[__sn.length-1];(function() {var __nd = document.createElement('script'); __nd.type = 'text/javascript'; __nd.async = true; __nd.src = ('http://') + 'goodies.softnep.com/nepali_date/nep.date.js'; __sn.parentNode.insertBefore(__nd, __sn);})();</script>&nbsp;|&nbsp; <span id="sn_nepalitime"></span></span>
        <script type="text/javascript">
        var currenttime = "<?php echo date('M-d,Y h:i:s');?>"; <!--"November 20, 2013 13:19:32"-->
        var montharray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
        var numbers = Array("&#2406;", "&#2407;", "&#2408;", "&#2409;", "&#2410;", "&#2411;", "&#2412;", "&#2413;", "&#2414;", "&#2415;");
        var serverdate = new Date(currenttime);
        function padlength(what) {
            var output = (what.toString().length == 1) ? "0" + what : what
            return output
        }
        function displaytime() {
            //alert(currenttime);
            serverdate.setSeconds(serverdate.getSeconds() + 1)
            var datestring = montharray[serverdate.getMonth()] + " " + padlength(serverdate.getDate()) + ", " + serverdate.getFullYear()


            hr=padlength(serverdate.getHours());
            mi=padlength(serverdate.getMinutes()) ;
            se=padlength(serverdate.getSeconds());
            dd="PM";
               if (hr >= 12) {
                hr = hr-12;
                dd = "AM";
            }
            if (hr == 0) {
                hr = 12;
            }
            var timestring = hr + ":" + mi + ":" + se +" "+ dd;
                var arr = timestring.split("");
                for (i = 0; i < (arr.length-3); i++) {
                    if (arr[i] != ":") {
                        arr[i] = numbers[arr[i]];
                    } 
                }
                timestring = arr.join("");
        /*        timestring = timestring.replace("AM","gu");       
         timestring = timestring.replace("PM","gu");*/
                document.getElementById("sn_nepalitime").innerHTML = " " + timestring;

            setTimeout('displaytime()',1000);
        }
        displaytime();
        </script>


        <!-- END OF NEPALI DATE AND TIME -->
</div>

This prints : Wednesday February 5, 2014 | बुधवार, माघ २२, २०७० | ०७:३६:४५ PM

And i want it to print : 2070-10-22

Answer

&#193;lvaro Gonz&#225;lez picture Álvaro González · Feb 4, 2014

Here's a little trick to obtain dates in the Nepali language:

<?php

header('Content-Type: text/plain; charset=utf-8');

$formats = array(
    IntlDateFormatter::NONE => 'IntlDateFormatter::NONE',
    IntlDateFormatter::SHORT => 'IntlDateFormatter::SHORT',
    IntlDateFormatter::MEDIUM => 'IntlDateFormatter::MEDIUM',
    IntlDateFormatter::LONG => 'IntlDateFormatter::LONG',
    IntlDateFormatter::FULL => 'IntlDateFormatter::FULL',
);
$locale = 'ne_NP';
$time_zone = 'Asia/Katmandu';

foreach($formats as $format => $label){
    echo $label . PHP_EOL;
    $fmt = new IntlDateFormatter($locale, $format, $format, $time_zone, IntlDateFormatter::GREGORIAN);
    echo $fmt->format(new DateTime) . PHP_EOL;
    echo PHP_EOL;
}
IntlDateFormatter::NONE
२०१४०२०४ ०६:१७ उत्तर मध्यान्ह

IntlDateFormatter::SHORT
२०१४-०२-०४ १८:१७

IntlDateFormatter::MEDIUM
२०१४ फेब ४ १८:१७:०९

IntlDateFormatter::LONG
२०१४ फेब्रुअरी ४ १८:१७:०९ GMT+५:४५

IntlDateFormatter::FULL
२०१४ फेब्रुअरी ४, मङ्गलबार १८:१७:०९ GMT+०५:४५