Programming, tutorials, mechatronics, operating systems, and other tech stuff

Menghitung jumlah hari antara 2 tanggal di PHP

4 comments
Sebuah coretan singkat tentang cara untuk menghitung jumlah hari antara 2 tanggal di PHP. Fungsi ini memang sepele, tapi tetap saya post disini karena siapa tahu ada yang membutuhkan.

Penerapannya bisa untuk menghitung tengat waktu/deadline, kadaluarsa, jangka waktu, dll.

<?php

function hitungHari($awal,$akhir){

$tglAwal = strtotime($awal);

$tglAkhir = strtotime($akhir);

$jeda = abs($tglAkhir - $tglAwal);

return floor($jeda/(60*60*24));

}

?>
untuk penggunaanya (dalam contoh ini menggunakan format penanggalan SQL):

echo hitungHari('2015-01-01','2015-12-01');

Penjelasannya


function hitungHari($awal,$akhir)

adalah deklarasi fungsi hitungHari yg menerima 2 parameter, yaitu variabel $awal dan $akhir. Dimana $awal adalah tanggal awal (dalam kasus ini menggunakan format string penanggalan SQL yaitu tahun-bulan-tanggal). Dan $akhir adalah tanggal akhir.

$tglAwal = strtotime($awal);
$tglAkhir = strtotime($akhir);

Dua baris diatas mengubah variabel $awal dan $akhir yg berisi string tanggal, kedalam bentuk time (jumlah detik sejak tahun 1 Januari 1970) yang kemudian disimpan ke variable $tglAwal dan $tglAkhir.

$jeda = abs($tglAkhir - $tglAwal);

Baris diatas adalah operasi pengurangan dari $tglAkhir dikurangi $tglAwal. Sehingga didapat hasil yaitu jarak waktu antara 2 variabel tadi dalam satuan detik. Fungsi abs() adalah untuk mendapatkan angka absolut (angka yg selalu positif) karena bisa saja hasil dari pengurangan tersebut adalah minus, jika $tglAwal lebih besar daripada $tglAkhir. Lalu kemudian disimpan kedalam variabel $jeda.

return floor($jeda/(60*60*24));
Fungsi floor() pada baris diatas adalah untuk membulatkan kebawah dari hasil perhitungan $jeda/(60*60*24). Maksud perhitungan disamping adalah membagi nilai variabel $jeda dengan (60*60*24) dimana 60 adalah jumlah detik dalam 1 menit, 60 adalah jumah menit dalam 1 jam, 24 adalah jumlah jam dalam 1 hari. Karena nilai dari $jeda adalah dalam satuan detik.

Jika anda ingin menyesuaikan fungsi tersebut untuk menghitung jumlah minggu, bulan, tahun, tinggal anda sesuaikan (60*60*24) menjadi (60*60*24*7) untuk minggu,  (60*60*24*30) untuk bulan.

Sekian tutorial PHP sederhana untuk menghitung jumlah hari antara 2 tanggal. Semoga bermanfaat.

4 comments :

  1. Crypto-currency as a modern form of the digital asset has received a worldwide acclaim for easy and faster financial transactions and its awareness among people have allowed them to take more interest in the field thus opening up new and advanced ways of making payments. Crypto.com Referral Code with the growing demand of this global phenomenon more,new traders and business owners are now willing to invest in this currency platform despite its fluctuating prices however it is quite difficult to choose the best one when the market is full. In the list of crypto-currencies bit-coins is one of the oldest and more popular Crypto.com Referral Code for the last few years. It is basically used for trading goods and services and has become the part of the so-called computerized block-chain system allowing anyone to use it thus increasing the craze among the public, Crypto.com Referral Code.

    Common people who are willing to purchase BTC can use an online wallet system for buying them safely in exchange of cash or credit cards and in a comfortable way from the thousands of BTC foundations around the world and keep them as assets for the future. Due to its popularity, many corporate investors are now accepting them as cross-border payments and the rise is unstoppable. With the advent of the internet and mobile devices,information gathering has become quite easy as a result the BTC financial transactions are accessible and its price is set in accordance with people’s choice and preferences thus leading to a profitable investment with Crypto.com Referral Code Code. Recent surveys have also proved that instability is good for BTC exchange as if there is instability and political unrest in the country due to which banks suffer then investing in BTC can surely be a better option. Again bit-coin transaction fees are pretty cheaper and a more convenient technology for making contracts thus attracting the crowd. The BTC can also be converted into different fiat currencies and is used for trading of securities, for land titles, document stamping, public rewards and vice versa.

    Another advanced block-chain project is Ethereumor the ETH which has served much more than just a digital form of crypto-currency Crypto.com Referral Code and its popularity in the last few decades have allowed billions of people to hold wallets for them. With the ease of the online world,the ETH have allowed the retailers and business organizations to accept them for trading purposes, therefore, can serve as the future of the financial system.

    ReplyDelete
  2. Our full Lace Front Wigs are all hand made with a lace cap. They are manufactured with thin lace sewn on top of the cap. Individual hairs are then sewn onto the thin lace. Each lace wig has lace all around the unit which will need to be cut prior to securing the wig to your head. You will need to cut along the hairline around your entire head. By doing so, you will be able to wear your hair anyway you like. You can even style ponytails, up-dos, etc. Once the Lace Wigs is successfully applied, it will appear that all the hair is growing directly from your head!

    Lace front wigs are hand-made with lace front cap & machine weft at back. Lace front wigs are manufactured with a thin lace that extends from ear to ear across the hairline. When you receive the wig, the lace will be quite long in the front. Cut and style according to your preference, as you will need to apply adhesive along the front of the wig. Once the wig is applied, you will still have Lace Wigs with a very natural appearance.
    TeamWigz Provide the Best Lace Front Wigs and Lace Wigs in Johannesburg and South Africa.

    ReplyDelete
  3. 360Guide was born from passion for action sports, passion for writing and interest in the Internet. We have been snowboarding and surfing for a long time. We also enjoy wakeboarding and mountain biking and actually any sport that gives you adrenaline, soul, lifestyle, puts you outside and connects you with nature. Surfboard For Beginners
    From all the sports mentioned on this website, surfing is our biggest love. Since conditions over here are pretty bad, much more energy and inner fire must be given to the sport. Surfing freezing cold waters, shitty windswells, making our own surfboards and traveling all over the world… all this is behind us and we are looking forward to more of the same.

    Snowboarding is our second love… there is nothing more beautiful than powder covered mountains on a crisp bluebird sky morning. And all others are not far behind.

    The main goal of this website is to provide people with information on each of these sports. Info on equipment, choosing the right stuff, what matters and what doesn’t. A few words on the history of sport, where do we come from and where are we going. Tips on technique, how to guides for beginners. Basically all the things you wished you knew when you were just starting out and all the important info that will help you make an informed buy of your next surfboard, snowboard or whatever it is that you will be buying. The most important guides, tips and info can be found directly from the drop down menu at the top of the site. For the rest of the articles click on the desired sport in the same menu and a list of all articles related to that sport will be shown. Like at the end of this about us section everything linked to this site is displayed.

    ReplyDelete