Kategori
PHP

The Use of Date/Time Function in PHP

Case Study : Using the the Date/Time Function in PHP
Requirements : Webserver Package (XAMPP, EasyPHP, APPServ), already installed.

If you developing PHP application, the date function is one of the function that will used most often. The simple example is, we will use this function to count some people’s age from her/his birth.

Here are some examples of the use of the date / time function that is often used in develop applications with php.

1. Showing Current Date in various formats

Asumption : Today is Thursday, 25 November 2010

  • Format : 2010-11-25
    The code is : date("Y-m-d");
  • Format : November 25, 2010, 8:17 PM
    Code : date("F j, Y, g:i a");

For more details about the output format of date, see the following table (click on the table to see table more clearly):

Output Date Format

2. Calculating Range/Date differences

Script:

<?php
function datediff($tgl1, $tgl2){
$tgl1 = strtotime($tgl1);
$tgl2 = strtotime($tgl2);
$diff_secs = abs($tgl1 - $tgl2);
$base_year = min(date("Y", $tgl1), date("Y", $tgl2));
$diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
return array( "years" => date("Y", $diff) - $base_year, "months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1, "months" => date("n", $diff) - 1, "days_total" => floor($diff_secs / (3600 * 24)), "days" => date("j", $diff) - 1, "hours_total" => floor($diff_secs / 3600), "hours" => date("G", $diff), "minutes_total" => floor($diff_secs / 60), "minutes" => (int) date("i", $diff), "seconds_total" => $diff_secs, "seconds" => (int) date("s", $diff) );
}
$tgl1 = '1989-06-06';
$tgl2 = date("Y/m/d/ h:m:s");
$a = datediff($tgl1, $tgl2);
echo 'tanggal 1 = '.$tgl1; echo '<br>';
echo 'tanggal 2 = '.$tgl2; echo '<br>';
echo 'Selisih = '.$a[years].' tahun '.$a[months].' bulan '.$a[days].' hari '.$a[hours].' jam '.$a[minutes].' menit '.$a[seconds].' detik';
?>

This script will produce output like the example image below:

Difference Between 2 Date
3. Date Adding

In the script below we will try to add days, months, and years at current date so as to produce output in the form of a certain date results of the addition.

Code :

<?php
$todayDate = date("Y-m-d");// current date
echo "Today: ".$todayDate."<br>";
$now = strtotime(date("Y-m-d"));
//Add one day to today
$date = date('Y-m-j', strtotime('+1 day', $now));
echo "After adding 1 day = ".$date."<br>";
$addMonth = 5;
//Add variabel addMonth to today
$date2 = date('Y-m-j', strtotime('+'.$addMonth.' month', $now));
echo "After adding $addMonth month = ".$date2."<br>";
//Add 6 year to today
$date3 = date('Y-m-j', strtotime('+6 year', $now));
echo "After adding 6 year = ".$date3."<br>";
?>

Output dari script diatas adalah seperti tertampil pada gambar dibawah :

Date Adding

4. Knowing Whether Today is the Last Day of the Month

Script :

<?php
// If today is the last day of the month, do echo
if(date('j') == date('t')) {
echo 'Today is the Last Day of this month';
}
else echo 'Today is NOT the Last Day of this month';
?>

Output :

Last Date ?

5. Calculating remaining days in this year

Script :

<?php
$today = getdate();
$yday = ($today['yday']);
$leap = date('L');
$tdl = $leap + 365;
$dliy = $tdl - $yday;
echo "Dari hari ini ".date('Y-m-d ').", Kita masih memiliki sisa " . $dliy . " hari dalam tahun ini.";
?>

Output :

Day Remain in Year

Hope that several examples from the use of date function above will be useful for all. I myself often use it, of course.

Okay, Happy Coding 🙂

Kategori
PHP

Penggunaan Fungsi Date / Time pada PHP

Studi Kasus : Menggunakan fungsi Date/Time pada PHP
Kebutuhan : Webserver Package (XAMPP, EasyPHP, APPServ), already installed.

Dalam mendevelop aplikasi PHP, fungsi date akan sering sekali digunakan. Contoh sederhananya, kita akan menggunakan fungsi date untuk menghitung usia.

Berikut ini adalah beberapa contoh penggunaan fungsi date/time yang sering digunakan dalam develop aplikasi dengan php.

1. Menampilkan Hari Ini ( Current Date ) dengan berbagai format

Asumsi : hari ini adalah Kamis, 25 November 2010

  • Format : 2010-11-25
    Codenya adalah : date("Y-m-d");
  • Format : November 25, 2010, 8:17 PM
    Code : date("F j, Y, g:i a");

Untuk lebih lengkapnya tentang output format dari date, lihat pada tabel berikut (klik pada tabel untuk melihat tabel dengan lebih jelas) :

Output Date Format

2. Penghitungan Rentang/Selisih Tanggal

Script:

<?php
function datediff($tgl1, $tgl2){
$tgl1 = strtotime($tgl1);
$tgl2 = strtotime($tgl2);
$diff_secs = abs($tgl1 - $tgl2);
$base_year = min(date("Y", $tgl1), date("Y", $tgl2));
$diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
return array( "years" => date("Y", $diff) - $base_year, "months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1, "months" => date("n", $diff) - 1, "days_total" => floor($diff_secs / (3600 * 24)), "days" => date("j", $diff) - 1, "hours_total" => floor($diff_secs / 3600), "hours" => date("G", $diff), "minutes_total" => floor($diff_secs / 60), "minutes" => (int) date("i", $diff), "seconds_total" => $diff_secs, "seconds" => (int) date("s", $diff) );
}
$tgl1 = '1989-06-06';
$tgl2 = date("Y/m/d/ h:m:s");
$a = datediff($tgl1, $tgl2);
echo 'tanggal 1 = '.$tgl1; echo '<br>';
echo 'tanggal 2 = '.$tgl2; echo '<br>';
echo 'Selisih = '.$a[years].' tahun '.$a[months].' bulan '.$a[days].' hari '.$a[hours].' jam '.$a[minutes].' menit '.$a[seconds].' detik';
?>

Script diatas akan menghasilkan contoh output seperti gambar dibawah ini :

Difference Between 2 Date
3. Penambahan Tanggal

Pda script dibawah kita akan mencoba menambahkan hari, bulan, dan tahun pada current date ( tanggal sekarang ) sehingga menghasilkan output berupa tanggal tertentu hasil penambahan.

Code :

<?php
$todayDate = date("Y-m-d");// current date
echo "Today: ".$todayDate."<br>";
$now = strtotime(date("Y-m-d"));
//Add one day to today
$date = date('Y-m-j', strtotime('+1 day', $now));
echo "After adding 1 day = ".$date."<br>";
$addMonth = 5;
//Add variabel addMonth to today
$date2 = date('Y-m-j', strtotime('+'.$addMonth.' month', $now));
echo "After adding $addMonth month = ".$date2."<br>";
//Add 6 year to today
$date3 = date('Y-m-j', strtotime('+6 year', $now));
echo "After adding 6 year = ".$date3."<br>";
?>

Output dari script diatas adalah seperti tertampil pada gambar dibawah :

Date Adding

4. Mengetahui Apakah hari ini adalah Hari Terakhir Bulan ini

Script :

<?php
// If today is the last day of the month, do echo
if(date('j') == date('t')) {
echo 'Today is the Last Day of this month';
}
else echo 'Today is NOT the Last Day of this month';
?>

Output :

Last Date ?

5. Menghitung sisa hari dalam Tahun ini

Script :

<?php
$today = getdate();
$yday = ($today['yday']);
$leap = date('L');
$tdl = $leap + 365;
$dliy = $tdl - $yday;
echo "Dari hari ini ".date('Y-m-d ').", Kita masih memiliki sisa " . $dliy . " hari dalam tahun ini.";
?>

Output :

Day Remain in Year

Semoga contoh – contoh penggunaan fungsi date di atas akan berguna bagi teman – teman. Saya sendiri sering menggunakannya, tentu saja.

Okay, Happy Coding 🙂