Masalah di setlocale ini adalah tampilan koma (comma) dan titik (dot) yang tidak konsisten. Disatu waktu bisa muncul hasilnya dengan koma, dilain waktu muncul dengan titik.

Untuk contoh kasus saya akan buat database dengan tabel

CREATE TABLE `dataharga` (
  `id` INT(11) NOT NULL,
  `harga_beli` FLOAT NOT NULL,
  `harga_jual` FLOAT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
--
-- Dumping data for table `dataharga`
--
 
INSERT INTO `dataharga` (`id`, `harga_beli`, `harga_jual`) VALUES
(1, 10000, 15000),
(2, 9800.24, 15000),
(3, 9700, 14999.9);

Disini saya menggunakan Laravel, dan solusi ini bisa digunakan untuk PHP murni maupun yang menggunakan framework (Codeigniter, Laravel, Slim Framework,Yii Framework dll).
Contoh skrip sederhana

Route::get('/', function () {
    echo "<pre>";
    $harga = \DB::table('dataharga')->select('harga_beli','harga_jual')->get();
    print_r($harga);
});

tampilan script diatas dibrowser

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [harga_beli] => 10000
                    [harga_jual] => 15000
                )
 
            [1] => stdClass Object
                (
                    [harga_beli] => 9800.24
                    [harga_jual] => 15000
                )
 
            [2] => stdClass Object
                (
                    [harga_beli] => 9700
                    [harga_jual] => 14999.9
                )
 
        )
 
)

hasil diatas adalah hasil yang diharapkan. Coba tambahkan setlocale diatas

Route::get('/', function () {
    setlocale(LC_ALL, 'id_ID.UTF8', 'id_ID.UTF-8', 'id_ID.8859-1', 'id_ID', 'en_US.UTF8', 'en_US.UTF-8', 'en_US');
    echo "<pre>";
    $harga = \DB::table('dataharga')->select('harga_beli','harga_jual')->get();
    print_r($harga);
});

hasilnya menjadi

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [harga_beli] => 10000
                    [harga_jual] => 15000
                )
 
            [1] => stdClass Object
                (
                    [harga_beli] => 9800,24
                    [harga_jual] => 15000
                )
 
            [2] => stdClass Object
                (
                    [harga_beli] => 9700
                    [harga_jual] => 14999,9
                )
 
        )
 
)

lihat di 9800,24 dan 14999,9. Yang ditampilkan menggunakn koma, padahal data yang dipanggil dari database harusnya menggunakan titik. Hasil diatas baik dengan raw query ataupun eloquent.
contoh set locale

Route::get('/', function () {
    echo "<pre>";
    $harga = \DB::table('dataharga')->select('harga_beli','harga_jual')->get();
    print_r($harga);
    echo "<h3>Set Locale</h3>";
    setlocale(LC_ALL, 'id_ID.UTF8', 'id_ID.UTF-8', 'id_ID.8859-1', 'id_ID', 'en_US.UTF8', 'en_US.UTF-8', 'en_US');
    setlocale(LC_ALL, 'id_ID.UTF8', 'id_ID.UTF-8', 'id_ID.8859-1', 'id_ID', 'en_US.UTF8', 'en_US.UTF-8', 'en_US');
    $harga = \DB::table('dataharga')->select('harga_beli','harga_jual')->get();
    print_r($harga);
    $harga = \DB::table('dataharga')->select('harga_beli','harga_jual')->get();
    print_r($harga);
});

outputnya

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [harga_beli] => 10000
                    [harga_jual] => 15000
                )
 
            [1] => stdClass Object
                (
                    [harga_beli] => 9800.24
                    [harga_jual] => 15000
                )
 
            [2] => stdClass Object
                (
                    [harga_beli] => 9700
                    [harga_jual] => 14999.9
                )
        )
)
Set Locale
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [harga_beli] => 10000
                    [harga_jual] => 15000
                )
 
            [1] => stdClass Object
                (
                    [harga_beli] => 9800,24
                    [harga_jual] => 15000
                )
 
            [2] => stdClass Object
                (
                    [harga_beli] => 9700
                    [harga_jual] => 14999,9
                )
        )
)
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [harga_beli] => 10000
                    [harga_jual] => 15000
                )
 
            [1] => stdClass Object
                (
                    [harga_beli] => 9800,24
                    [harga_jual] => 15000
                )
 
            [2] => stdClass Object
                (
                    [harga_beli] => 9700
                    [harga_jual] => 14999,9
                )
        )
)

Kalo hanya dengan satu kasus diatas, belum terlalu kelihatan efek sampingnya. Contoh anda membuat suatu helper untuk membuat tanggal Indonesia, dan di set di app/Providers/AppServiceProvider.php. Kita menggunakan blade untuk menampilkan data, kurang lebih layout standarnya seperti ini

@extends('layouts.app')
 
@section('title', 'Page Title')
 
@section('sidebar')
    @parent
 
    <p>This is appended to the master sidebar.</p>
@endsection
 
@section('content')
    <p>This is my body content.</p>
@endsection

Bila dd($harga) di atas @section('content') hasilnya dengan titik, tetapi bila dibawahnya akan menggunakan koma. Dengan kata lain setiap pemanggilan data setelah inisiasi setlocale dilakukan akan merubah data yang ditampilkan (contoh koma/titik, format tanggal).

Solusi untuk permasalahan ini adalah dengan mereset setlocale ke settingan semula. Seperti yang dibuat di manual setlocale php

If locale is NULL or the empty string “”, the locale names will be set from the values of environment variables with the same names as the above categories, or from “LANG”.

Ubah script diatas menjadi

Route::get('/', function () {
    echo "<pre>";
    $harga = \DB::table('dataharga')->select('harga_beli','harga_jual')->get();
    print_r($harga);
    echo "<h3>Set Locale</h3>";
    setlocale(LC_ALL, 'id_ID.UTF8', 'id_ID.UTF-8', 'id_ID.8859-1', 'id_ID', 'en_US.UTF8', 'en_US.UTF-8', 'en_US');
    setlocale(LC_ALL, 'id_ID.UTF8', 'id_ID.UTF-8', 'id_ID.8859-1', 'id_ID', 'en_US.UTF8', 'en_US.UTF-8', 'en_US');
    $harga = \DB::table('dataharga')->select('harga_beli','harga_jual')->get();
    print_r($harga);
    setlocale(LC_ALL, '');
    $harga = \DB::table('dataharga')->select('harga_beli','harga_jual')->get();
    print_r($harga);
});

yang ditambahkan adalah setlocale(LC_ALL, '');, hasil eksekusi scriptnya

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [harga_beli] => 10000
                    [harga_jual] => 15000
                )
 
            [1] => stdClass Object
                (
                    [harga_beli] => 9800.24
                    [harga_jual] => 15000
                )
 
            [2] => stdClass Object
                (
                    [harga_beli] => 9700
                    [harga_jual] => 14999.9
                )
 
        )
 
)
Set Locale
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [harga_beli] => 10000
                    [harga_jual] => 15000
                )
 
            [1] => stdClass Object
                (
                    [harga_beli] => 9800,24
                    [harga_jual] => 15000
                )
 
            [2] => stdClass Object
                (
                    [harga_beli] => 9700
                    [harga_jual] => 14999,9
                )
 
        )
 
)
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => stdClass Object
                (
                    [harga_beli] => 10000
                    [harga_jual] => 15000
                )
 
            [1] => stdClass Object
                (
                    [harga_beli] => 9800.24
                    [harga_jual] => 15000
                )
 
            [2] => stdClass Object
                (
                    [harga_beli] => 9700
                    [harga_jual] => 14999.9
                )
 
        )
 
)

begitulah efek samping setlocale dan solusinya di php.

Leave a comment

Your email address will not be published. Required fields are marked *