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 πŸ™‚

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

3 replies on “The Use of Date/Time Function in PHP”

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.