Mencari selisih tanggal di PHP cepat dan mudah

Bismillaahirrohmaanirrohiim…

Untuk mencari selisih tanggal di PHP dengan cepat dan mudah kita akan menggunakan fungsi “DateTime“.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$tgla = '2022-06-15';
$tglb = '2022-07-15';
$tgl1 = new DateTime($tgla);
$tgl2 = new DateTime($tglb);
$selisih = $tgl2->diff($tgl1);
echo $selisih->days;//30
echo 'Selisih waktu 2 tanggal: ';
echo $selisih->y . ' tahun, ';
echo $selisih->m . ' bulan, ';
echo $selisih->d . ' hari, ';
echo $selisih->h . ' jam, ';
echo $selisih->i . ' menit, ';
echo $selisih->s . ' detik, ';
//Selisih waktu 2 tanggal: 0 tahun, 1 bulan, 0 hari, 0 jam, 0 menit, 0 detik,
echo 'Total selisih hari : ' . $selisih->days;
//Total selisih hari : 30
$tgla = '2022-06-15'; $tglb = '2022-07-15'; $tgl1 = new DateTime($tgla); $tgl2 = new DateTime($tglb); $selisih = $tgl2->diff($tgl1); echo $selisih->days;//30 echo 'Selisih waktu 2 tanggal: '; echo $selisih->y . ' tahun, '; echo $selisih->m . ' bulan, '; echo $selisih->d . ' hari, '; echo $selisih->h . ' jam, '; echo $selisih->i . ' menit, '; echo $selisih->s . ' detik, '; //Selisih waktu 2 tanggal: 0 tahun, 1 bulan, 0 hari, 0 jam, 0 menit, 0 detik, echo 'Total selisih hari : ' . $selisih->days; //Total selisih hari : 30
$tgla = '2022-06-15';
$tglb = '2022-07-15';

$tgl1 = new DateTime($tgla);
$tgl2 = new DateTime($tglb);
$selisih = $tgl2->diff($tgl1);

echo $selisih->days;//30
echo 'Selisih waktu 2 tanggal: ';
echo $selisih->y . ' tahun, ';
echo $selisih->m . ' bulan, ';
echo $selisih->d . ' hari, ';
echo $selisih->h . ' jam, ';
echo $selisih->i . ' menit, ';
echo $selisih->s . ' detik, ';
//Selisih waktu 2 tanggal: 0 tahun, 1 bulan, 0 hari, 0 jam, 0 menit, 0 detik, 

echo 'Total selisih hari : ' . $selisih->days;
//Total selisih hari : 30

Dengan menggunakan “DateTime” kita langsung bisa mengetahui selisih tanggal, bulan, tahun, bahkan sampai detik.

Selanjutnya untuk mendapatkan selisih tanggal dalam bentuk array, tinggal kita buat fungsinya seperti berikut:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function rangeDates($startDate, $endDate) {
$dates = [];
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
for ($i = $startDate; $i <= $endDate;$i += (86400)) {
$dates[] = date('Y-m-d', $i);
}
return $dates;
}
$tgla = '2022-06-15';
$tglb = '2022-07-15';
$rangeDates = rangeDates($tgla, $tglb);
function rangeDates($startDate, $endDate) { $dates = []; $startDate = strtotime($startDate); $endDate = strtotime($endDate); for ($i = $startDate; $i <= $endDate;$i += (86400)) { $dates[] = date('Y-m-d', $i); } return $dates; } $tgla = '2022-06-15'; $tglb = '2022-07-15'; $rangeDates = rangeDates($tgla, $tglb);
function rangeDates($startDate, $endDate) {
    $dates = [];
        
    $startDate = strtotime($startDate);
    $endDate = strtotime($endDate);
         
    for ($i = $startDate; $i <= $endDate;$i += (86400)) {
        $dates[] = date('Y-m-d', $i);
    }

    return $dates;
}
$tgla = '2022-06-15';
$tglb = '2022-07-15';

$rangeDates = rangeDates($tgla, $tglb);

Hasilnya:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Array
(
[0] => 2022-06-15
[1] => 2022-06-16
[2] => 2022-06-17
[3] => 2022-06-18
[4] => 2022-06-19
[5] => 2022-06-20
[6] => 2022-06-21
[7] => 2022-06-22
[8] => 2022-06-23
[9] => 2022-06-24
[10] => 2022-06-25
[11] => 2022-06-26
[12] => 2022-06-27
[13] => 2022-06-28
[14] => 2022-06-29
[15] => 2022-06-30
[16] => 2022-07-01
[17] => 2022-07-02
[18] => 2022-07-03
[19] => 2022-07-04
[20] => 2022-07-05
[21] => 2022-07-06
[22] => 2022-07-07
[23] => 2022-07-08
[24] => 2022-07-09
[25] => 2022-07-10
[26] => 2022-07-11
[27] => 2022-07-12
[28] => 2022-07-13
[29] => 2022-07-14
[30] => 2022-07-15
)
Array ( [0] => 2022-06-15 [1] => 2022-06-16 [2] => 2022-06-17 [3] => 2022-06-18 [4] => 2022-06-19 [5] => 2022-06-20 [6] => 2022-06-21 [7] => 2022-06-22 [8] => 2022-06-23 [9] => 2022-06-24 [10] => 2022-06-25 [11] => 2022-06-26 [12] => 2022-06-27 [13] => 2022-06-28 [14] => 2022-06-29 [15] => 2022-06-30 [16] => 2022-07-01 [17] => 2022-07-02 [18] => 2022-07-03 [19] => 2022-07-04 [20] => 2022-07-05 [21] => 2022-07-06 [22] => 2022-07-07 [23] => 2022-07-08 [24] => 2022-07-09 [25] => 2022-07-10 [26] => 2022-07-11 [27] => 2022-07-12 [28] => 2022-07-13 [29] => 2022-07-14 [30] => 2022-07-15 )
Array
(
    [0] => 2022-06-15
    [1] => 2022-06-16
    [2] => 2022-06-17
    [3] => 2022-06-18
    [4] => 2022-06-19
    [5] => 2022-06-20
    [6] => 2022-06-21
    [7] => 2022-06-22
    [8] => 2022-06-23
    [9] => 2022-06-24
    [10] => 2022-06-25
    [11] => 2022-06-26
    [12] => 2022-06-27
    [13] => 2022-06-28
    [14] => 2022-06-29
    [15] => 2022-06-30
    [16] => 2022-07-01
    [17] => 2022-07-02
    [18] => 2022-07-03
    [19] => 2022-07-04
    [20] => 2022-07-05
    [21] => 2022-07-06
    [22] => 2022-07-07
    [23] => 2022-07-08
    [24] => 2022-07-09
    [25] => 2022-07-10
    [26] => 2022-07-11
    [27] => 2022-07-12
    [28] => 2022-07-13
    [29] => 2022-07-14
    [30] => 2022-07-15
)

Nah, mudah bukan….

Baca Juga:   Membuat CRUD dengan PHP MySQL HTML CSS JS (part 7)

Semoga bermanfaat.

Bagikan

You May Also Like

About the Author: rasupe

Leave a Reply

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