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

Tags: , , , , , , , , ,

Comments

  1. Ini menjadi referensi saya. Terima kasih

    Reply
  2. Genะ“ยฉrico De Cialis
    (Admin)
    Cialis 5 mg prezzo cialis 5 mg prezzo tadalafil 5 mg prezzo

    Reply
  3. ์†Œ๋‹‰์นด์ง€๋…ธ ๋งํฌ
    February 18, 2024 - 10:31 pm

    saungsantoso.com
    Fang Jifan์€ ๋จธ๋ฆฌ ๋’ค์—์„œ ์˜คํ•œ์„ ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค.

    Reply
  4. 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

    Reply
  5. ์—๊ทธ ๋ฒณ
    February 22, 2024 - 8:05 am

    sm-online-game.com
    ํ•˜์ง€๋งŒ ๊ทธ์˜ ์†์— ๋“  ์ฑ…์€… ์ฒธ ์‹œ์‚ฐ์˜ ๋ˆˆ์—๋Š” ์™„์ „ํžˆ ๋‹ฌ๋ž๋‹ค!

    Reply
  6. pragmatic-ko.com
    “…”Xiao Jing์€ ๋งˆ์Œ ์†์œผ๋กœ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์™•์„ธ์ž๋Š” ์ž์‹ ์„ ๋ฏธ์›Œํ• ๊นŒ์š”? ์•„์ง๋„ Fang Jifan์„ ์‹ซ์–ดํ•ฉ๋‹ˆ๊นŒ?

    Reply
  7. ์—๊ทธ๋ฒณ
    February 27, 2024 - 4:26 pm

    sm-online-game.com
    ๋งˆ์นจ๋‚ด ๋•…์— ๋‚ด๋ ค๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์„ ๋•Œ ๊ทธ๋Š” ์ฆ‰์‹œ ์ฃผ์ธ์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค.

    Reply
  8. pragmatic-ko.com
    ์™•๋ถ€์‹œ๋Š” ๋ˆˆ์— ๋„๋Š” ์˜์ƒ์„ ์ž…๊ณ  ๋Œ€๊ธฐ์‹ค๋กœ ๋Œ๊ณ  ๊ฐ”๋‹ค.

    Reply
  9. smcasino-game.com
    February 28, 2024 - 2:25 pm

    dota2answers.com
    “์˜ค.” Li Chaoxian์€ ์ฃผ์ €ํ•˜์ง€ ์•Š๊ณ  ๋น„ ์†์œผ๋กœ ๋Œ์ง„ํ–ˆ์Šต๋‹ˆ๋‹ค.

    Reply
  10. Can you tell us more about this? I’d care to find out some additional information.
    slots starburst

    Reply
  11. 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

    Reply
  12. pragmatic-ko.com
    March 3, 2024 - 2:39 am

    lfchungary.com
    ๊ทธ๋ž˜์„œ ๊ทธ๋Š” ๋‹ต์„ ์•„์ฃผ ์ง„์ง€ํ•˜๊ฒŒ ๊บผ๋‚ด์„œ ๋‹ค์‹œ ๋น„๊ตํ–ˆ๋‹ค.

    Reply
  13. ์—๊ทธ๋ฒณ์Šฌ๋กฏ
    March 3, 2024 - 10:40 am

    bistroduet.com
    ๋‹ค๋ฅธ ํ•œ๋ฆฐ์€ ๋ชจ๋‘ ๋†€๋ผ์„œ ์™•๋ถ€์‹œ๋ฅผ ๋ฐ”๋ผ๋ณด์•˜๋‹ค.

    Reply
  14. pragmatic-ko.com
    March 4, 2024 - 7:41 am

    pragmatic-ko.com
    “๋ฅ˜ ๋‚ด์‹œ๋Š” ์ถ”๋„์‹์—์„œ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ๋” ์‹ฌ๊ฐํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.”

    Reply
  15. smcasino-game.com
    March 5, 2024 - 11:40 am

    smcasino7.com
    ํ™ฉํƒœํ›„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด, ๋‚˜๋Š”…

    Reply
  16. raytalktech.com
    ์„œ๋ถ€๋กœ์˜ ํ•ญํ•ด๋Š” ๋˜ํ•œ ํฌ๊ท€ํ•œ ๋ณด๋ฌผ๊ณผ ๋ถ€์— ๋Œ€ํ•œ ํ•„์š”์„ฑ์„ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

    Reply
  17. Tadalafil 20 Mg Para Que Sirve Precio
    You have hit the mark.
    Cialis 5 mg prezzo cialis 5 mg prezzo cialis 5 mg prezzo

    Reply
  18. ์Šฌ๋กฏ์‚ฌ์ดํŠธ
    March 11, 2024 - 5:40 pm

    andrejpos.com
    Zeng Jie๋Š” ํ‰์ •์„ ๋˜์ฐพ๊ณ  Xiao Jing์—๊ฒŒ ์ธ์‚ฌํ•˜๊ณ  ๋– ๋‚ฌ์Šต๋‹ˆ๋‹ค.

    Reply
  19. 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/

    Reply
  20. 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/

    Reply
  21. 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/

    Reply
  22. ???๋ฉ”๊ฐ€์Šฌ๋กฏ ์กฐ์ž‘
    March 17, 2024 - 4:48 am

    sm-casino1.com
    ๋ฆฌ๋” ์™•์›ฌ์œ ๋Š” ๋ˆˆ์•ž์˜ ๋ชจ๋“  ๊ฒƒ์„ ์–ด๋ฆฌ๋‘ฅ์ ˆํ•˜๊ฒŒ ๋ฐ”๋ผ๋ณด๋‹ค๊ฐ€ ๊ฐ‘์ž๊ธฐ ์›ƒ์—ˆ๋‹ค.

    Reply
  23. ์—๊ทธ๋ฒณ ์Šฌ๋กฏ
    March 17, 2024 - 11:31 pm

    pactam2.com
    ๊ทธ๋Š” Xiao Jing์„ ๋ฐ”๋ผ ๋ณผ ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. “Xiao Banban, ์™•์ž๊ฐ€ ์—ฌ๊ธฐ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌด์—‡์„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๊นŒ?”

    Reply
  24. 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

    Reply
  25. ???๋ฉ”๊ฐ€์Šฌ๋กฏ ์กฐ์ž‘
    March 20, 2024 - 12:37 am

    mega-slot77.com
    Fang Jifan์€ ๋ฏธ์†Œ๋ฅผ ์ง€์œผ๋ฉฐ ๋งํ–ˆ์Šต๋‹ˆ๋‹ค. “์˜ˆ, ํํ•˜.”

    Reply
  26. ttbslot.com
    ๋„์‚ด๋œ ๋ผ์ง€ ๊ฐ™์€ ์šธ๋ถ€์ง–์Œ๊ณผ ํ•จ๊ป˜ Fang Jifan์€ ๋ฐ˜์Œ ํ›„์— ์นจ๋ฌตํ–ˆ์Šต๋‹ˆ๋‹ค.

    Reply
  27. 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

    Reply
  28. fpparisshop.com
    March 28, 2024 - 4:31 am

    usareallservice.com
    ้žๅธธใซๆœ‰็›Šใชๅ†…ๅฎนใงใ—ใŸใ€‚ๆฌกๅ›žไฝœใ‚‚ๆฅฝใ—ใฟใซใ—ใฆใ„ใพใ™ใ€‚

    Reply
  29. 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.

    Reply
  30. vpn coupon 2024
    April 3, 2024 - 11:20 am

    Incredible points. Solid arguments. Keep up the amazing spirit.

    Reply
  31. order amoxil sale – generic ceftin 250mg baycip generic

    Reply
  32. 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.

    Reply
  33. sandyterrace.com
    April 9, 2024 - 6:25 am

    largestcatbreed.com
    Liu Wenshan์€ ๋งค์šฐ ๋ฐ”๋นด๊ณ  ๋„ˆ๋ฌด ๋ฐ”๋น ์„œ ๋•…์— ๋‹ฟ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

    Reply
  34. qiyezp.com
    ์–ด๋–ค ์‚ฌ๋žŒ๋“ค์€ ์™•์„ธ์ž ์ „ํ•˜๊ฐ€ ์ž์‹ ์˜ ์ผ์„ ์ œ๋Œ€๋กœ ํ•˜๊ณ  ์žˆ์ง€ ์•Š๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    Reply
  35. Pretty! This has been a really wonderful post. Many thanks for providing these details.

    Reply
  36. very informative articles or reviews at this time.

    Reply
  37. cleocin online – oxytetracycline tablet oral chloromycetin

    Reply
  38. 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

    Reply
  39. purchase albuterol inhalator generic – order albuterol 4mg pills order theophylline for sale

    Reply
  40. fpparisshop.com
    April 15, 2024 - 4:10 am

    toasterovensplus.com
    ใจใฆใ‚‚่ˆˆๅ‘ณๆทฑใ„ๅ†…ๅฎนใงใ—ใŸใ€‚่ชญใ‚€ใฎใ‚’ๆฅฝใ—ใฟใซใ—ใฆใ„ใพใ™ใ€‚

    Reply
  41. 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

    Reply
  42. purchase medrol generic – fml-forte over the counter azelastine nasal spray

    Reply
  43. ์—๊ทธ๋ฒณ ์Šฌ๋กฏ
    April 15, 2024 - 9:16 pm

    sandyterrace.com
    “๋„ค, ์ •๋ง ์œ ๊ฐ์ž…๋‹ˆ๋‹ค.” ์™•์ง„์›์€ ๊ณ ๊ฐœ๋ฅผ ์ €์œผ๋ฉฐ ํ•œ์ˆจ์„ ์‰ฌ์—ˆ๋‹ค.

    Reply
  44. order desloratadine 5mg generic – ketotifen 1 mg price albuterol inhaler

    Reply
  45. fpparisshop.com
    April 18, 2024 - 4:39 am

    exprimegranada.com
    ๅฎŸ็”จ็š„ใงๅฝนใซ็ซ‹ใคๆƒ…ๅ ฑใŒ่ฑŠๅฏŒใงใ€ๅคงๅค‰ๆบ€่ถณใ—ใฆใ„ใพใ™ใ€‚

    Reply
  46. Great information shared.. really enjoyed reading this post thank you author for sharing this post .. appreciated

    Reply
  47. order semaglutide online – brand glucovance generic desmopressin

    Reply
  48. Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.

    Reply
  49. fpparisshop.com
    April 24, 2024 - 10:08 am

    cougarsbkjersey.com
    ่ชญใ‚“ใงใ„ใฆใ€ๅฎŸ็”จ็š„ใชๆƒ…ๅ ฑใŒๅคšใใฆ้žๅธธใซๅ‚่€ƒใซใชใ‚Šใพใ—ใŸใ€‚

    Reply
  50. brand ketoconazole – order generic lotrisone cheap sporanox 100mg

    Reply
  51. เธ”เธนเธšเธญเธฅเธชเธ”
    April 25, 2024 - 1:20 pm

    I like the efforts you have put in this, regards for all the great content.

    Reply
  52. sandyterrace.com
    April 25, 2024 - 4:42 pm

    thewiin.com
    ํ”ผ๋ฅผ ํ† ํ•œ ์™•์ขŒ๋Š” ๊นœ์ง ๋†€๋ผ ํ™์น˜์ œ๋ฅผ ๋ฐ”๋ผ๋ณด๋ฉฐ ๊นœ์ง ๋†€๋ž๋‹ค.

    Reply
  53. 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.

    Reply
  54. digoxin sale – irbesartan 150mg generic lasix tablet

    Reply

Leave a Reply to TimsothyfupCancel reply

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