How to set Indonesian date using PHP?

Posted on

We need date format for our report, or check to customer, or something. Any legal documents need to show with date included. In my country, Indonesia, the date format have a little different with other. Format and spelling have some unique to other country. I run using PHP Framework called CodeIgniter. So, I’ll tell you how to set indonesian date format using php.

In Indonesia, basic date format you’ll find like this : Senin, 20 Januari 2017. Just for example. We need to set:

First, Day name we’ll convert to Bahasa Indonesia.

Second, We’ll convert Month name to Bahasa Indonesia,

Thirth, We just need to show up and echo the function result. Here is the complete function.

function indonesian_date ($timestamp = '', $date_format = 'l, j F Y | H:i', $suffix = 'WIB') {
    if (trim ($timestamp) == '')
    {
            $timestamp = time ();
    }
    elseif (!ctype_digit ($timestamp))
    {
        $timestamp = strtotime ($timestamp);
    }
    # remove S (st,nd,rd,th) there are no such things in indonesia :p
    $date_format = preg_replace ("/S/", "", $date_format);
    $pattern = array (
        '/Mon[^day]/','/Tue[^sday]/','/Wed[^nesday]/','/Thu[^rsday]/',
        '/Fri[^day]/','/Sat[^urday]/','/Sun[^day]/','/Monday/','/Tuesday/',
        '/Wednesday/','/Thursday/','/Friday/','/Saturday/','/Sunday/',
        '/Jan[^uary]/','/Feb[^ruary]/','/Mar[^ch]/','/Apr[^il]/','/May/',
        '/Jun[^e]/','/Jul[^y]/','/Aug[^ust]/','/Sep[^tember]/','/Oct[^ober]/',
        '/Nov[^ember]/','/Dec[^ember]/','/January/','/February/','/March/',
        '/April/','/June/','/July/','/August/','/September/','/October/',
        '/November/','/December/',
    );
    $replace = array ( 'Sen','Sel','Rab','Kam','Jum','Sab','Min',
        'Senin','Selasa','Rabu','Kamis','Jumat','Sabtu','Minggu',
        'Jan','Feb','Mar','Apr','Mei','Jun','Jul','Ags','Sep','Okt','Nov','Des',
        'Januari','Februari','Maret','April','Juni','Juli','Agustus','Sepember',
        'Oktober','November','Desember',
    );
    $date = date ($date_format, $timestamp);
    $date = preg_replace ($pattern, $replace, $date);
    $date = "{$date} {$suffix}";
    return $date;
}

I found the function on internet. the function result show the date with day, month name in Indonesia, and also give suffix WIB (Waktu Indonesia Barat) extension.

How to use?

Easily, just call the function with parameter variable include. This function running with timestamp, date format, and suffix. If you will not use the timestamp, just keep it empty. And the date wee can improve $date_format = ‘l, j F Y as we need.

About optional format result, you can improve this code

$date = date ($date_format, $timestamp);

And the end of result, look at this code

 $date = "{$date} {$suffix}";

If you don’t like using suffix, just remove and anything you can do seriously.

Baca juga  Tutorial: Using HighChart on CodeIgniter project without ajax

I think enought, for any discussion we can Q&A at the comment form.

_ Sutriman

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments