Bagaimana Cara Mengambil Tanggal Sekarang di PHP?

Dibuat
·
Dilihat22 kali
0

Saya memiliki variabel $currentTime dimana saya menulis tanggal secara statis (tidak menggunakan tanggal saat kode dieksekusi), bagaimana caranya mengambil tanggal sekarang di PHP agar variabel $currentTime berubah-ubah menggunakan waktu dinamis sesuai dengan waktu kode dieksekusi?

Berikut sampel kode yang saya gunakan:

$currentTime = "31 January 2024 14:26:24 WIB";

echo "The current time is: $currentTime";
// The current time is: 31 January 2024 14:26:24 WIB
1 Jawaban
0

Gunakan fungsi date(string $format) yang mengambil waktu dinamis (waktu saat kode dieksekusi), contoh:

$currentTime = date("j F Y G:i:s T");
echo "The current time is: $currentTime"; 
// The current time is: 31 January 2024 14:35:11 WIB

Fungsi date() menerima parameter format yang menentukan format output waktu, berikutnya beberapa contohnya:

echo date("F j, Y, g:i a") . "\n";                 // January 31, 2024, 2:17 pm
echo date("m.d.y") . "\n";                         // 01.31.24
echo date("j, n, Y") . "\n";                       // 31, 1, 2024
echo date("Ymd") . "\n";                           // 20240131
echo date('h-i-s, j-m-y, it is w Day') . "\n";     // 02-26-24, 31-01-24, 2631 2624 3 Wedpm24
echo date('\i\t \i\s \t\h\e jS \d\a\y.') . "\n";   // it is the 31st day.
echo date("D M j G:i:s T Y") . "\n";               // Wed Jan 31 14:26:24 WIB 2024
echo date('H:m:s \m \i\s\ \m\o\n\t\h') . "\n";     // 14:01:24 m is month
echo date("H:i:s") . "\n";                         // 14:26:24
echo date("Y-m-d H:i:s") . "\n";                   // 2024-01-31 14:26:24

Untuk cara menulis format nya, cek di sini: https://www.php.net/manual/en/datetime.format.php

Dibuat

Kamu tau jawabannya?

Ayo bergabung bersama lebih dari 200.000 pengguna lainnya!