Selamat Pagi, Kali ini kita berada di matakuliah Pemrograman Web PHP, pertemuan kedelapan besama saya Ian Lubis. Pokok pembahasan kita kali ini ialah, Date, Time, File. DATE Fungsi date mengembalikan sebuah string terformat yang diisikan pada argument. Argument tersebut berfungsi untuk mengambil nilai apa saja yang akan di kembalikan. contoh: // asumsikan hari ini March 10th, 2001, 5:16:18 pm echo date("m.d.y"); // 03.10.01 PHP memberikan banyak nilai argument yang dapat kita modifikasi penggunaannya, berikut argument yang di tawarkan PHP. d format character D Description Day --- --- Day of the month, 2 digits with leading zeros A textual representation of a day, three letters Example returned values 01 to 31 j Day of the month without leading zeros 1 to 31 l (lowercase 'L') N S w Mon through Sun A full textual representation of the day of Sunday through the week Saturday ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) English ordinal suffix for the day of the month, 2 characters Numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday) st, nd, rd or th. Works well with j 0 (for Sunday) through 6 (for Saturday) z The day of the year (starting from 0) 0 through 365 Week --- --- ian lubis Page 1
format character W F m M n Description Example returned values ISO-8601 week number of year, weeks Example: 42 (the 42nd starting on Monday (added in PHP 4.1.0) week in the year) Month --- --- A full textual representation of a month, such as January or March Numeric representation of a month, with leading zeros A short textual representation of a month, three letters Numeric representation of a month, without leading zeros January through December 01 through 12 Jan through Dec 1 through 12 t Number of days in the given month 28 through 31 L o Y Year --- --- Whether it's a leap year ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) A full numeric representation of a year, 4 digits 1 if it is a leap year, 0 otherwise. Examples: 1999 or 2003 Examples: 1999 or 2003 y A two digit representation of a year Examples: 99 or 03 a A Time --- --- Lowercase Ante meridiem and Post meridiem Uppercase Ante meridiem and Post meridiem am or pm AM or PM B Swatch Internet time 000 through 999 g G h H 12-hour format of an hour without leading zeros 24-hour format of an hour without leading zeros 12-hour format of an hour with leading zeros 24-hour format of an hour with leading zeros 1 through 12 0 through 23 01 through 12 00 through 23 ian lubis Page 2
format character Description i Minutes with leading zeros 00 to 59 Example returned values s Seconds, with leading zeros 00 through 59 e Timezone --- --- I (capital i) O P Examples: UTC, GMT, Timezone identifier (added in PHP 5.1.0) Atlantic/Azores Whether or not the date is in daylight saving time Difference to Greenwich time (GMT) in hours Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) 1 if Daylight Saving Time, 0 otherwise. Example: +0200 Example: +02:00 T Timezone setting of this machine Examples: EST, MDT... Z Full Date/Time Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. --- --- c ISO 8601 date (added in PHP 5) r U RFC 2822 formatted date -43200 through 50400 2004-02-12T15:19:21+0 0:00 Example: Thu, 21 Dec 2000 16:01:07 +0200 Seconds since the Unix Epoch (January 1 See also time() 1970 00:00:00 GMT) ian lubis Page 3
contoh penggunaan lainnya: // asumsikan hari ini March 10th, 2001, 5:16:18 pm echo date("f j, Y, g:i a"); // March 10, 2001, 5:16 pm echo date("m.d.y"); // 03.10.01 echo date("j, n, Y"); // 10, 3, 2001 echo date("ymd"); // 20010310 echo date('h-i-s, j-m-y, it is w Day z '); // 05-16-17, 10-03-01, 1631 1618 6 Fripm01 echo date('\i\t \i\s \t\h\e js \d\a\y.'); // It is the 10th day. echo date("d M j G:i:s T Y"); // Sat Mar 10 15:16:08 MST 2001 echo date('h:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:17 m is month echo date("h:i:s"); // 17:16:17 TIME Fungsi yang mengembalikan nilai waktu sekarang dengan ukuran jumlah detik $nextweek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs echo 'Now: '. date('y-m-d')."\n"; echo 'Next Week: '. date('y-m-d', $nextweek)."\n"; FILE PHP memberikan keluasan kepada para developer untuk memodifikasi sebuah file baik mengedit, delete atau menambahkan sesuatu atau membuat file baru. Terdapat sebuah ian lubis Page 4
fasilitas yang di berikan oleh PHP, dengan metode FOPEN. Di beberapa perusahaan hosting penggunaan FOPEN biasanya di non aktivkan, dengan alasan keamanan. Namun anda dapat memintanya untuk di bukakan tentunya dengan resiko yang akan di tangung sendiri. Sebagai contoh, kita akan membuat sebuah file *.txt baru dan menambahkan beberapa baris tulisan. $handle = fopen("file.txt", "w"); fwrite($handle, "belajar PHP"); Simpan file di atas dan lalu buka pada browser, setelah itu lihat pada direktori tempat anda menyimpan file tersebut, maka akan muncul sebuah file baru file.txt, lalu buka pada notepad maka tulisan "belajar PHP" akan muncul pada file tersebut. ian lubis Page 5
Membaca isi file $filename = "file.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); echo $contents; Maka ketika anda menjalankan scripts di atas, akan muncul isi dari file "file.txt", kita asmumsikan bahwa file tersebut terisikan tulisan "Belajar PHP", maka tulan yang muncul ialah "Belajar PHP". Terima kasih atas pertemuan kali ini. Salam Terima kasih atas pertemuan kali ini. Salam ian lubis Page 6