Operasi Tanggal Jam Strtotime/DateTime pada PHP
Poin pembahasan :
– Memformat tanggal jam
– Menambah atau mengurangi tanggal
– Membandingkan mana yang lebih dari atau kurang dari
– Mencari selisih dua tanggal
– Mencari daftar tanggal diantara dua tanggal
– Mencari selisih dua tanggal
– Validasi penulisan tanggal jam
Strtotime merupakan fungsi yang disediakan oleh php yang menurut saya syuper sekali fungsi ini, karena dengan satu fungsi ini kita bisa menyelesaikan beberapa masalah yang berkaitan dengan tanggal dan jam.
Misalnya kita ingin mengetahui lima hari lagi itu tanggal berapa ya? mungkin sebagian dari kita akan menggunakan cara tanggal sekarang + 5, misalnya sekarang tgl 5 + 5 berarti tanggal 10. Tapi akan masalah jika sekarang tgl 30, kan tidak mungkin tanggal 35, hehehe.
Sebelum melanjutkan, apakah Anda sudah tau arti Y-m-d H:i:s atau d F Y? bagi yang belum tau saya sarankan untuk membaca Date Manual terlebih dahulu supaya tidak bingung nantinya.
# Memformat tanggal jam
Misalkan kita punya data tanggal seperti ini 2017-09-01 11:20:55 dan ingin menampilkan tanggal tersebut menjadi 01 september 2017, 11:20 maka kita cukup melakukan seperti ini:
<?php $data = "2017-09-01 11:20:55"; echo date("d F Y, H:i", strtotime($data)); ?>
Ketika menjalankan strtotime($data) yang terjadi adalah php akan mengubah tanggal tersebut menjadi format timestamp seperti ini 1504239655, setiap tanggal/waktu memiliki unix timestamp. Ketika data sudah berbentuk timestamp maka data tersebut menjadi fleksibel untuk ditampilkan dalam format apa saja.
# Menambah atau mengurangi tanggal
Misalkan kita akan mencari tahu tanggal berapakah 5 hari kedepan, caranya seperti ini:
<?php $time_sekarang = time(); echo date("d F Y", strtotime("+5 days", $time_sekarang)); ?>
Maka akan didapati tanggal yang benar untuk lima hari kedepan dari sekarang.
Untuk mendapatkan timestamp waktu sekarang caranya bisa memakai fungsi time() atau strtotime(“now”) atau juga strtotime(date(“Y-m-d H:i:s”)). Sedangkan +5 days ini maksutnya menambahkan waktu yang ada diparameter kedua sebanyak 5 hari.
Parameter pertama bisa min atau plus tergantung kita ingin mengurangi atau menambahkan, dan bisa days, month, years, hours, minutes, seconds.
# Membandingkan mana yang lebih dari atau kurang dari
Untuk membandingkan tanggal, caranya cukup ubah tanggal menjadi format timestamp kemudian bandingkan, contoh:
<?php $tgl1 = "2017-10-10"; $tgl2 = "2017-10-11"; if (strtotime($tgl2) > strtotime($tgl1)) { echo "tgl 2 lebih dari tgl 1"; } ?>
Jadi format timestamp bisa digunakan untuk perbandingan.
Menggunakan DateTime
Selain menggunakan strtotime, alternatif lainnya adalah menggunakan DateTime, DateTime mulai diperkenalkan sejak versi 5.2, saat ini rata-rata versi php yang sering dipakai sudah lebih dari 5.3, jadi saya rasa tidak ada masalah dengan DateTime.
Berikut contoh penggunakan DateTime:
<?php //Memformat tanggal jam $date1 = "2017-09-01 11:20:55"; $date = new DateTime($date1); echo $date->format("d F Y, H:i"); //Menambah tanggal $date1 = "2017-08-30"; $date = new DateTime($date1); $date_plus = $date->modify("+5 days"); echo $date_plus->format("Y-m-d"); //Mengurangi tanggal $date_min = $date->modify("-3 month"); echo $date_min->format("Y-m-d"); //Membandingkan $date1 = new DateTime("2017-08-04"); $date2 = new DateTime("2017-08-06"); if ($date2 > $date1) { echo "ok"; }
Contoh kasus lain yang lebih komplek:
# Mencari daftar tanggal yang ada diantara dua tanggal
<?php $begin = new DateTime('2017-08-01'); $end = new DateTime('2017-08-05'); $end = $end->modify('+1 day'); $interval = new DateInterval('P1D'); //referensi : https://en.wikipedia.org/wiki/ISO_8601#Durations $daterange = new DatePeriod($begin, $interval ,$end); foreach ($daterange as $date) { echo $date->format("Y-m-d") . "<br>"; }
# Mencari selisih dua tanggal
$begin = new DateTime('2017-08-01 23:00'); $end = new DateTime('2017-08-05 21:22'); $diff = $begin->diff($end); echo "Selisihnya adalah : " . $diff->format("%d hari %h jam %i menit");
Hal lain yang sering kita butuhkan saat berurusan dengan tanggal dan jam adalah validasi penulisan tanggal dan jam, karena sebelum data dimasukkan ke database yang fieldnya bertipe DATETIME, data harus divalidasi dulu supaya bener. Berikut # Validasi penulisan tanggal jam
<?php function valid_datetime($datetime) { if (preg_match('/^(19[0-9]{2}|2[0-9]{3})-(0[1-9]|1[012])-([123]0|[012][1-9]|31) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/', $datetime)) { return true; } return false; } $tgl = "2017-08-29 23:20:59"; if (valid_datetime($tgl)) { echo "valid"; } ?>
Sekian posting kali ini, semoga Anda menemukan apa yang sedang dicari ๐
Oh ya jangan lupa like FB page Dokumenary atau subscribe ya…
Terimakasih
Category: Uncategorized
Psikotes Online
January 28, 2023 - 3:33 pm
Ini menjadi referensi saya. Terima kasih
Timsothyfup
February 11, 2024 - 7:24 pm
Genะยฉrico De Cialis
(Admin)
Cialis 5 mg prezzo cialis 5 mg prezzo tadalafil 5 mg prezzo
์๋์นด์ง๋ ธ ๋งํฌ
February 18, 2024 - 10:31 pm
saungsantoso.com
Fang Jifan์ ๋จธ๋ฆฌ ๋ค์์ ์คํ์ ๋๊ผ์ต๋๋ค.
DichaelVok
February 22, 2024 - 6:37 am
Thank you, I have recently been searching for information approximately this subject for a while and yours is the greatest I’ve discovered till now. However, what concerning the conclusion? Are you sure concerning the source?
game bitcoin
์๊ทธ ๋ฒณ
February 22, 2024 - 8:05 am
sm-online-game.com
ํ์ง๋ง ๊ทธ์ ์์ ๋ ์ฑ ์… ์ฒธ ์์ฐ์ ๋์๋ ์์ ํ ๋ฌ๋๋ค!
lfchungary.com
February 24, 2024 - 8:25 am
pragmatic-ko.com
“…”Xiao Jing์ ๋ง์ ์์ผ๋ก ์๊ฐํ์ต๋๋ค. ์์ธ์๋ ์์ ์ ๋ฏธ์ํ ๊น์? ์์ง๋ Fang Jifan์ ์ซ์ดํฉ๋๊น?
์๊ทธ๋ฒณ
February 27, 2024 - 4:26 pm
sm-online-game.com
๋ง์นจ๋ด ๋ ์ ๋ด๋ ค๊ฐ ์ ์๊ฒ ๋์์ ๋ ๊ทธ๋ ์ฆ์ ์ฃผ์ธ์ ์ฐพ์์ต๋๋ค.
pragmatic-ko.com
February 28, 2024 - 12:47 pm
pragmatic-ko.com
์๋ถ์๋ ๋์ ๋๋ ์์์ ์ ๊ณ ๋๊ธฐ์ค๋ก ๋๊ณ ๊ฐ๋ค.
smcasino-game.com
February 28, 2024 - 2:25 pm
dota2answers.com
“์ค.” Li Chaoxian์ ์ฃผ์ ํ์ง ์๊ณ ๋น ์์ผ๋ก ๋์งํ์ต๋๋ค.
Lewislap
February 29, 2024 - 12:50 am
Can you tell us more about this? I’d care to find out some additional information.
slots starburst
Timsothyfup
March 2, 2024 - 3:34 pm
Howdy! This is kind of off topic but I need some advice from an established blog. Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about making my own but I’m not sure where to start. Do you have any points or suggestions? Many thanks
hellspin bonus
pragmatic-ko.com
March 3, 2024 - 2:39 am
lfchungary.com
๊ทธ๋์ ๊ทธ๋ ๋ต์ ์์ฃผ ์ง์งํ๊ฒ ๊บผ๋ด์ ๋ค์ ๋น๊ตํ๋ค.
์๊ทธ๋ฒณ์ฌ๋กฏ
March 3, 2024 - 10:40 am
bistroduet.com
๋ค๋ฅธ ํ๋ฆฐ์ ๋ชจ๋ ๋๋ผ์ ์๋ถ์๋ฅผ ๋ฐ๋ผ๋ณด์๋ค.
pragmatic-ko.com
March 4, 2024 - 7:41 am
pragmatic-ko.com
“๋ฅ ๋ด์๋ ์ถ๋์์์ ์ธ๊ธํ์ง ์์์ง๋ง ๋ ์ฌ๊ฐํด์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค.”
smcasino-game.com
March 5, 2024 - 11:40 am
smcasino7.com
ํฉํํ๋ฅผ ๊ตฌํ ์ ์๋ค๋ฉด, ๋๋…
Carloshep
March 9, 2024 - 5:55 pm
Blogs about psychology and relationships between men and women
khasiss.com
March 9, 2024 - 7:11 pm
raytalktech.com
์๋ถ๋ก์ ํญํด๋ ๋ํ ํฌ๊ทํ ๋ณด๋ฌผ๊ณผ ๋ถ์ ๋ํ ํ์์ฑ์ ๊ฐ์ ธ์ฌ ๊ฒ์ ๋๋ค.
Fobertfut
March 10, 2024 - 4:23 pm
Tadalafil 20 Mg Para Que Sirve Precio
You have hit the mark.
Cialis 5 mg prezzo cialis 5 mg prezzo cialis 5 mg prezzo
์ฌ๋กฏ์ฌ์ดํธ
March 11, 2024 - 5:40 pm
andrejpos.com
Zeng Jie๋ ํ์ ์ ๋์ฐพ๊ณ Xiao Jing์๊ฒ ์ธ์ฌํ๊ณ ๋ ๋ฌ์ต๋๋ค.
Lewislap
March 14, 2024 - 4:21 am
Thank you a bunch for sharing this with all people you really recognize what you’re speaking approximately! Bookmarked. Kindly additionally visit my website =). We can have a link alternate contract among us
http://whatsnextwithmoney.biz/__media__/js/netsoltrademark.php?d=hottelecom.biz/hi/
Fobertfut
March 14, 2024 - 5:06 pm
I’d like to find out more? I’d like to find out some additional information.
http://microlearn.biz/__media__/js/netsoltrademark.php?d=hottelecom.biz/hi/
Fobertfut
March 15, 2024 - 4:04 pm
Helpful info. Lucky me I discovered your website accidentally, and I’m stunned why this twist of fate did not took place earlier! I bookmarked it.
http://alt1.toolbarqueries.google.co.zm/url?q=https://didvirtualnumbers.com/de/
???๋ฉ๊ฐ์ฌ๋กฏ ์กฐ์
March 17, 2024 - 4:48 am
sm-casino1.com
๋ฆฌ๋ ์์ฌ์ ๋ ๋์์ ๋ชจ๋ ๊ฒ์ ์ด๋ฆฌ๋ฅ์ ํ๊ฒ ๋ฐ๋ผ๋ณด๋ค๊ฐ ๊ฐ์๊ธฐ ์์๋ค.
์๊ทธ๋ฒณ ์ฌ๋กฏ
March 17, 2024 - 11:31 pm
pactam2.com
๊ทธ๋ Xiao Jing์ ๋ฐ๋ผ ๋ณผ ์ ์์์ต๋๋ค. “Xiao Banban, ์์๊ฐ ์ฌ๊ธฐ ์์ต๋๋ค. ๋ฌด์์ํ๊ณ ์์ต๋๊น?”
Ismaelopeld
March 18, 2024 - 4:02 pm
Helpful information. Fortunate me I discovered your web site by accident, and I am stunned why this accident didn’t happened in advance! I bookmarked it.
https://lpgtech.ua/lovato
???๋ฉ๊ฐ์ฌ๋กฏ ์กฐ์
March 20, 2024 - 12:37 am
mega-slot77.com
Fang Jifan์ ๋ฏธ์๋ฅผ ์ง์ผ๋ฉฐ ๋งํ์ต๋๋ค. “์, ํํ.”
qiyezp.com
March 22, 2024 - 8:26 pm
ttbslot.com
๋์ด๋ ๋ผ์ง ๊ฐ์ ์ธ๋ถ์ง์๊ณผ ํจ๊ป Fang Jifan์ ๋ฐ์ ํ์ ์นจ๋ฌตํ์ต๋๋ค.
LhaneVet
March 22, 2024 - 9:42 pm
I blog quite often and I truly thank you for your content. The article has truly peaked my interest. I am going to bookmark your website and keep checking for new details about once per week. I subscribed to your RSS feed as well.
Rybelsus
fpparisshop.com
March 28, 2024 - 4:31 am
usareallservice.com
้ๅธธใซๆ็ใชๅ ๅฎนใงใใใๆฌกๅไฝใๆฅฝใใฟใซใใฆใใพใใ
vpn code 2024
March 29, 2024 - 12:37 pm
Great post. I was checking continuously this blog and I’m impressed!
Very useful info specially the final section ๐ I care for such information much.
I used to be seeking this certain info for a long time.
Thank you and good luck.
vpn coupon 2024
April 3, 2024 - 11:20 am
Incredible points. Solid arguments. Keep up the amazing spirit.
Qfltns
April 4, 2024 - 4:38 pm
order amoxil sale – generic ceftin 250mg baycip generic
vpn special coupon
April 5, 2024 - 10:47 am
Great post. I was checking continuously this blog and I’m impressed!
Very useful information particularly the last part
๐ I care for such info a lot. I was looking for this
particular info for a long time. Thank you and good luck.
sandyterrace.com
April 9, 2024 - 6:25 am
largestcatbreed.com
Liu Wenshan์ ๋งค์ฐ ๋ฐ๋นด๊ณ ๋๋ฌด ๋ฐ๋น ์ ๋ ์ ๋ฟ์ง ์์์ต๋๋ค.
qiyezp.com
April 9, 2024 - 8:08 am
qiyezp.com
์ด๋ค ์ฌ๋๋ค์ ์์ธ์ ์ ํ๊ฐ ์์ ์ ์ผ์ ์ ๋๋ก ํ๊ณ ์์ง ์๋ค๊ณ ์๊ฐํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
ufaland
April 9, 2024 - 12:41 pm
Pretty! This has been a really wonderful post. Many thanks for providing these details.
UFABET
April 9, 2024 - 5:07 pm
very informative articles or reviews at this time.
Xwflyp
April 10, 2024 - 2:37 am
cleocin online – oxytetracycline tablet oral chloromycetin
alay3d
April 11, 2024 - 10:37 am
Wow Thanks for this information i find it hard to uncover great tips out there when it comes to this topic appreciate for the post website
Lqhjvz
April 13, 2024 - 5:54 pm
purchase albuterol inhalator generic – order albuterol 4mg pills order theophylline for sale
fpparisshop.com
April 15, 2024 - 4:10 am
toasterovensplus.com
ใจใฆใ่ๅณๆทฑใๅ ๅฎนใงใใใ่ชญใใฎใๆฅฝใใฟใซใใฆใใพใใ
link alay4d
April 15, 2024 - 9:57 am
Wow Thanks for this guide i find it hard to find extremely good material out there when it comes to this topic appreciate for the site site
Sxwezf
April 15, 2024 - 3:21 pm
purchase medrol generic – fml-forte over the counter azelastine nasal spray
์๊ทธ๋ฒณ ์ฌ๋กฏ
April 15, 2024 - 9:16 pm
sandyterrace.com
“๋ค, ์ ๋ง ์ ๊ฐ์ ๋๋ค.” ์์ง์์ ๊ณ ๊ฐ๋ฅผ ์ ์ผ๋ฉฐ ํ์จ์ ์ฌ์๋ค.
Yozzwp
April 16, 2024 - 4:04 am
order desloratadine 5mg generic – ketotifen 1 mg price albuterol inhaler
fpparisshop.com
April 18, 2024 - 4:39 am
exprimegranada.com
ๅฎ็จ็ใงๅฝนใซ็ซใคๆ ๅ ฑใ่ฑๅฏใงใๅคงๅคๆบ่ถณใใฆใใพใใ
ufabet
April 18, 2024 - 6:03 pm
Great information shared.. really enjoyed reading this post thank you author for sharing this post .. appreciated
Rpcjlm
April 22, 2024 - 6:31 pm
order semaglutide online – brand glucovance generic desmopressin
ufabet
April 23, 2024 - 5:17 pm
Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
fpparisshop.com
April 24, 2024 - 10:08 am
cougarsbkjersey.com
่ชญใใงใใฆใๅฎ็จ็ใชๆ ๅ ฑใๅคใใฆ้ๅธธใซๅ่ใซใชใใพใใใ
Ycypeb
April 25, 2024 - 12:12 am
brand ketoconazole – order generic lotrisone cheap sporanox 100mg
เธเธนเธเธญเธฅเธชเธ
April 25, 2024 - 1:20 pm
I like the efforts you have put in this, regards for all the great content.
sandyterrace.com
April 25, 2024 - 4:42 pm
thewiin.com
ํผ๋ฅผ ํ ํ ์์ข๋ ๊น์ง ๋๋ผ ํ์น์ ๋ฅผ ๋ฐ๋ผ๋ณด๋ฉฐ ๊น์ง ๋๋๋ค.
result togel hari ini
April 27, 2024 - 2:43 am
Hi there, I wish for to subscribe for this website to
obtain most recent updates, thus where can i do it please help.
Usrkia
April 27, 2024 - 3:06 am
digoxin sale – irbesartan 150mg generic lasix tablet