Part 1📝 String Functions (ប្រភេទ Functions សម្រាប់ String)
PHP មាន string functions ជាច្រើន។ យើងចែកវាជា 7 ប្រភេទ (categories):
🔢 strlen(), empty(), isset()
Concept: Functions ទាំងនេះប្រើសម្រាប់ពិនិត្យ string: រាប់ប្រវែង, ពិនិត្យថាទទេហើឬអត់, ពិនិត្យថាមានតម្លៃហើឬអត់។
▶ Output:
strlen("Hello World") = 11
empty("") = true
isset($name) = false
📝 Practice:
តើ strlen("PHP is fun") ត្រឡប់តម្លៃអ្វី? ហេតអ្វី?
Hint: រាប់ characters ទាំងអស់រួមទាំង spaces
🔤 strtolower(), strtoupper(), ucfirst(), ucwords()
Concept: បម្លែងអក្សរធំ/តូច (change letter case)។ សំខាន់សម្រាប់ normalize user input។
▶ Output:
strtolower: hello world php
strtoupper: HELLO WORLD PHP
ucfirst: Hello
ucwords: Hello World Php
📝 Practice:
បើ user បញ្ចូល "jOHN doe" តើអ្នកប្រើ function អ្វីដើម្បីបង្ហាញថា "John Doe"?
Hint: ត្រូវប្រើ 2 functions រួមគ្នា
✂️ trim(), ltrim(), rtrim()
Concept: កាត់ whitespace (spaces, tabs, newlines) ចេញពី string។ សំខាន់នាស់សម្រាប់ clean user input!
▶ Output:
Original: "Hello PHP"
trim(): "Hello PHP"
ltrim(): "Hello PHP"
rtrim(): "Hello PHP"
📝 Practice:
តើ trim(" Hello ") ត្រឡប់អ្វី?
Hint: trim កាត់ whitespace ពីសងខាង
🔍 strpos() - Find Position
Concept: strpos() រកទីតាំង (position) នៃ substring ក្នុង string។
⚠️ ការប្រយ័ត្ន:
Position ចាប់ផ្តើមពី 0! ដូច្នេះ position 0 មិនមែន false ទេ! ត្រូវប្រើ === false មិនមែន == false!
▶ Output:
strpos("Hello World", "World") = 6
strpos("Hello World", "Hello") = 0 (position 0, NOT false!)
strpos("Hello World", "xyz") = false
📝 Practice:
ហេតអ្វីបានជា if(strpos("abc","a")) មិនដំណើរការត្រឹមត្រូវ?
Hint: strpos returns 0, if(0) = false
✂️ substr() - Extract Part of String
Concept: substr(\$string, \$start, \$length) កាត់យកផ្នែកមួយនៃ string។ start ចាប់ពី 0។
▶ Output:
substr($s, 0, 5) = Hello
substr($s, 6, 5) = World
substr($s, -3) = PHP
substr($s, 0, -4) = Hello World
📝 Practice:
តើ substr("Cambodia", 0, 3) = ?
Hint: កាត់ 3 អក្សរដំបូង
🔄 str_replace()
Concept: str_replace(\$search, \$replace, \$subject) ជំនួសអត្ថបទក្នុង string។
▶ Output:
str_replace("Java","PHP",$t) = I love PHP
str_replace(" ","_","Hello World") = Hello_World
📝 Practice:
តើ str_replace("World", "PHP", "Hello World") = ?
Hint: ជំនួស World ដោយ PHP
🔗 explode() / implode()
Concept: explode() បំបែក string ទៅជា array។ implode() ភ្ជាប់ array ត្រឡប់ទៅ string វិញ។
▶ Output:
explode: ["apple", "banana", "cherry"]
implode: apple - banana - cherry
📝 Practice:
តើ explode(" ", "I love PHP") ត្រឡប់ array អ្វី?
Hint: បំបែកដោយ space
Interactive🎮 String Playground
Part 2🕐 Date & Time ក្នុង PHP
PHP មាន functions ជាច្រើនសម្រាប់ធ្វើការជាមួយ date និង time។
📅 date() - Format Date/Time
Concept: date(\$format) បង្ហាញកាលបរិច្ឆេទនិងម៉ោងតាម format ដែលគែកំណត់។
▶ Output:
date("Y-m-d") = 2026-03-01
date("d/m/Y") = 01/03/2026
date("H:i:s") = 05:43:26
date("l, d F Y") = Sunday, 01 March 2026
date("d/m/Y H:i") = 01/03/2026 05:43
📝 Practice:
តើ date("Y") ត្រឡប់អ្វី?
Hint: Y = ឆ្នាំពេញ 4 ខ្ទង់
⏰ time() - Unix Timestamp
Concept: time() ត្រឡប់ Unix timestamp (ចំនួនវិនាទីពី 1 Jan 1970)។ ប្រើ date() ដើម្បីបម្លែងវា។
▶ Output:
time() = 1772318606
date("Y-m-d", $ts) = 2026-03-01
date("d/m/Y H:i:s", $ts) = 01/03/2026 05:43:26
📝 Practice:
Timestamp ជាអ្វី? តើអាចបម្លែងវាទៅ date បានទេ?
Hint: ប្រើ date() ជាមួយ timestamp
🔍 strtotime()
Concept: strtotime() បម្លែង date string ទៅជា timestamp។ អាចប្រើភាសាអង់គ្លេសដែរ។
▶ Output:
strtotime("2026-02-12") = 1770829200
strtotime("+7 days") = 2026-03-08
strtotime("next Monday") = 2026-03-02
strtotime("+1 month") = 2026-04-01
📝 Practice:
តើ date("Y-m-d", strtotime("+30 days")) = ?
Hint: បន្ថែម 30 ថ្ងៃពីថ្ងៃនេះ
📦 DateTime - Object-Oriented
Concept: DateTime class ជាវិធី OOP សម្រាប់ធ្វើការជាមួយ date/time។ អាចបន្ថែម/ដកថ្ងៃបាន។
▶ Output:
Now: 2026-03-01 05:43:26
+7 days: 2026-03-08
-30 days: 2026-01-30
Diff 2026-01-01 to 2026-12-31: 364 days
📝 Practice:
តើបង្កើត DateTime ដើម្បីបន្ថែម 10 ថ្ងៃ តើសរសេរយ៉ាងម៉េច?
Hint: ប្រើ add() ជាមួយ DateInterval
Part 3🌍 Timezone
🌍 Setting Timezone
Concept: Timezone = time zone of a region. Cambodia = Asia/Phnom_Penh (UTC+7). Server timezone may differ from user timezone. Always set timezone explicitly!
▶ Output:
🇰🇭 Asia/Phnom_Penh: 2026-03-01 05:43:26
🌐 UTC: 2026-02-28 22:43:26
🇯🇵 Asia/Tokyo: 2026-03-01 07:43:26
📝 Practice:
Cambodia (Phnom Penh) timezone differs from UTC by how many hours?
Reference📊 Format Characters Table
| Character | Meaning | Example (Now) |
|---|---|---|
Y |
Full year (4 digits) | 2026 |
y |
Short year (2 digits) | 26 |
m |
Month 01-12 | 03 |
n |
Month 1-12 | 3 |
F |
Month name full | March |
M |
Month name short | Mar |
d |
Day 01-31 | 01 |
j |
Day 1-31 | 1 |
l |
Day name full | Sunday |
D |
Day name short | Sun |
N |
ISO weekday 1-7 | 7 |
w |
Weekday 0-6 | 0 |
t |
Days in month | 31 |
H |
Hour 24h 00-23 | 05 |
h |
Hour 12h 01-12 | 05 |
i |
Minutes 00-59 | 43 |
s |
Seconds 00-59 | 26 |
A |
AM/PM | AM |
a |
am/pm | am |
U |
Unix timestamp | 1772318606 |
Interactive🎮 Date & Time Playground
✏️ Practice: String Functions (10 Items)
សំណួរទី 1: តើ strlen("Hello") ត្រឡប់តម្លៃអ្វី?
Hint: រាប់ចំនួន characters
សំណួរទី 2: តើ strtoupper("php") = ?
Hint: uppercase
សំណួរទី 3: តើ ucwords("hello world") = ?
Hint: អក្សរដំបូងនៃពាក្យនីមួយៗធំ
សំណួរទី 4: trim() ធ្វើអ្វី? ប្រើនៅពេលណា?
Hint: កាត់ whitespace
សំណួរទី 5: សរសេរកូដដើម្បី replace គ្រប់ spaces ដោយ underscores ក្នុង "Hello World PHP"
Hint: str_replace
សំណួរទី 6: តើ strpos("abcdef", "a") ត្រឡប់អ្វី? ហេតអ្វីត្រូវប្រើ === false?
Hint: position 0 មិនមែន false
សំណួរទី 7: តើ substr("Programming", 0, 7) = ?
Hint: កាត់ 7 អក្សរដំបូង
សំណួរទី 8: តើ explode(",", "a,b,c") return អ្វី?
Hint: split by comma
សំណួរទី 9: សរសេរកូដ: បម្លែង string " HELLO world " ទៅ "hello world" (កាត់ space + lowercase)
Hint: ប្រើ trim + strtolower
សំណួរទី 10: តើ implode("-", ["2026","02","12"]) = ?
Hint: join array
✏️ Practice: Date & Time (8 Items + 2 Mini-Challenges)
សំណួរទី 1: តើ date("Y") ត្រឡប់អ្វី?
Hint: Y = full year
សំណួរទី 2: តើ date("d/m/Y") output format អ្វី?
Hint: d=day, m=month, Y=year
សំណួរទី 3: តើ time() ត្រឡប់អ្វី?
Hint: Unix timestamp
សំណួរទី 4: តើបង្ហាញថ្ងៃ 1 ខែបន្តាប់ពីនេះ ដោយប្រើ strtotime សរសេរយ៉ាងម៉េច?
Hint: strtotime("+1 month")
សំណួរទី 5: Cambodia timezone ជាអ្វី?
Hint: Asia/...
សំណួរទី 6: Format character ដែលបង្ហាញ full month name ជាអ្វី?
Hint: មើល table ខាងលើ
សំណួរទី 7: តើបង្កើត DateTime ដើម្បីគណនាចំនួនថ្ងៃរវាង 2 dates?
Hint: diff() method
សំណួរទី 8: តើ h និង H ខុសគ្ណាជាអ្វី ក្នុង date format?
Hint: h=12h, H=24h
🏆 Mini-Challenges
Clean + Normalize user input: " jOHN DOE " → "John Doe"
Hint: Combine trim + strtolower + ucwords
<?php
$input = " jOHN DOE ";
$clean = ucwords(strtolower(trim($input)));
echo $clean; // "John Doe"
?>
Build "pretty date": Input "2026-02-12" → "Thursday, 12 February 2026 (Asia/Phnom_Penh)"
Hint: Use date_default_timezone_set + date + strtotime
<?php
$tz = "Asia/Phnom_Penh";
date_default_timezone_set($tz);
$input = "2026-02-12";
$ts = strtotime($input);
$pretty = date("l, d F Y", $ts) . " ($tz)";
echo $pretty;
// Thursday, 12 February 2026 (Asia/Phnom_Penh)
?>
🔑 Answer Key
String Functions Answers:
S1 strlen("Hello") = 5
S2 strtoupper("php") = "PHP"
S3 ucwords("hello world") = "Hello World"
S4 trim() removes whitespace from both sides of string
S5 str_replace(" ", "_", "Hello World PHP") = "Hello_World_PHP"
S6 strpos returns 0 (not false). Use === false to check
S7 substr("Programming", 0, 7) = "Program"
S8 explode(",", "a,b,c") = ["a", "b", "c"]
S9 strtolower(trim(" HELLO world ")) = "hello world"
S10 implode("-", ["2026","02","12"]) = "2026-02-12"
Date/Time Answers:
D1 date("Y") = "2026"
D2 date("d/m/Y") = "12/02/2026"
D3 time() returns Unix timestamp
D4 date("Y-m-d", strtotime("+1 month"))
D5 Asia/Phnom_Penh (UTC+7)
D6 F = full month name
D7 $d1->diff($d2)->days
D8 h = 12h (01-12), H = 24h (00-23)
Mini-Challenges:
MC1 ucwords(strtolower(trim($input)))
MC2 date_default_timezone_set($tz); echo date("l, d F Y", strtotime($input)) . " ($tz)";