forked from kokonior/PHP-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTime.php
49 lines (36 loc) · 1.13 KB
/
DateTime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
// class DateTime default dari php nya
$dateTime = new DateTime();
// ngubah date sama time
$dateTime->setDate(2003, 7, 24);
$dateTime->setTime(17, 12, 12);
// memanipulasi datetime interval
// ini akan menambah 1 taun
$dateTime->add(new DateInterval("P1Y"));
// ini akan mengurangi 1 bulan, ntah itu taun, tanggal dll
$minusOneMonth = new DateInterval("P1M");
$minusOneMonth->invert = true;
$dateTime->add($minusOneMonth);
var_dump($dateTime);
$now = new DateTime();
// timezone saat ini
var_dump($now);
// mengubah function dateTimeZone
$now->setTimezone(new DateTimeZone("Europe/Berlin"));
// timezon setelah diubah
var_dump($now);
// format datetime
// merubah datetime jadi representasi/tampilan string
$string = $now->format("Y-m-d H:i:s");
echo "Waktu saat ini : $string" . PHP_EOL;
// parse datetime
// mengkonversi dari string ke format input datetime yang telah di tentukan
$date = DateTime::createFromFormat("Y-m-d H:i:s", "2020-11-10 10:10:10", new DateTimeZone("Asia/Jakarta"));
// var_dump($date);
// pengecekan jika format / yang diinputkan salah
if($date){
var_dump($date);
} else {
echo "Format salah";
}
?>