📘 PHP Beginner: String Functions + Date & Time

Timezone + Format Characters

🎯 រៀន string functions និង date/time ដើម្បីប្រើក្នុង PHP

📋 ការណែនាំ (Instructions)

Part 1📝 String Functions (ប្រភេទ Functions សម្រាប់ String)

PHP មាន string functions ជាច្រើន។ យើងចែកវាជា 7 ប្រភេទ (categories):

Category 1: Length & Checking

🔢 strlen(), empty(), isset()

Concept: Functions ទាំងនេះប្រើសម្រាប់ពិនិត្យ string: រាប់ប្រវែង, ពិនិត្យថាទទេហើឬអត់, ពិនិត្យថាមានតម្លៃហើឬអត់។

<?php $name = "Hello World"; echo strlen($name); // 11 $empty = ""; echo empty($empty); // 1 (true) echo isset($name); // 1 (true) ?>

▶ Output:

strlen("Hello World") = 11
empty("") = true
isset($name) = false

📝 Practice:

តើ strlen("PHP is fun") ត្រឡប់តម្លៃអ្វី? ហេតអ្វី?

Hint: រាប់ characters ទាំងអស់រួមទាំង spaces

Answer: strlen("PHP is fun") = 10
Category 2: Case Conversion

🔤 strtolower(), strtoupper(), ucfirst(), ucwords()

Concept: បម្លែងអក្សរធំ/តូច (change letter case)។ សំខាន់សម្រាប់ normalize user input។

<?php $t = "hELLo wORLd PHP"; echo strtolower($t); // hello world php echo strtoupper($t); // HELLO WORLD PHP echo ucfirst("hello"); // Hello echo ucwords("hello world php"); // Hello World Php ?>

▶ 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 រួមគ្នា

Answer: ucwords(strtolower("jOHN doe")) = "John Doe"
Category 3: Trim / Cleaning

✂️ trim(), ltrim(), rtrim()

Concept: កាត់ whitespace (spaces, tabs, newlines) ចេញពី string។ សំខាន់នាស់សម្រាប់ clean user input!

<?php $d = " Hello PHP "; echo trim($d); // "Hello PHP" echo ltrim($d); // "Hello PHP " echo rtrim($d); // " Hello PHP" ?>

▶ Output:

Original: "Hello PHP"
trim(): "Hello PHP"
ltrim(): "Hello PHP"
rtrim(): "Hello PHP"

📝 Practice:

តើ trim(" Hello ") ត្រឡប់អ្វី?

Hint: trim កាត់ whitespace ពីសងខាង

Answer: trim(" Hello ") = "Hello"
Category 4: Searching

🔍 strpos() - Find Position

Concept: strpos() រកទីតាំង (position) នៃ substring ក្នុង string។

⚠️ ការប្រយ័ត្ន:

Position ចាប់ផ្តើមពី 0! ដូច្នេះ position 0 មិនមែន false ទេ! ត្រូវប្រើ === false មិនមែន == false!

<?php $t = "Hello World"; $p1 = strpos($t, "World"); // 6 $p2 = strpos($t, "Hello"); // 0 (NOT false!) $p3 = strpos($t, "xyz"); // false // CORRECT way: if (strpos($t, "Hello") !== false) { echo "Found!"; } ?>

▶ 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

Answer: strpos("abc","a") returns 0. if(0) = false! ត្រូវប្រើ if(strpos("abc","a") !== false)
Category 5: Substring

✂️ substr() - Extract Part of String

Concept: substr(\$string, \$start, \$length) កាត់យកផ្នែកមួយនៃ string។ start ចាប់ពី 0។

<?php $s = "Hello World PHP"; echo substr($s, 0, 5); // "Hello" echo substr($s, 6, 5); // "World" echo substr($s, -3); // "PHP" echo substr($s, 0, -4); // "Hello World" ?>

▶ 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 អក្សរដំបូង

Answer: substr("Cambodia", 0, 3) = "Cam"
Category 6: Replace

🔄 str_replace()

Concept: str_replace(\$search, \$replace, \$subject) ជំនួសអត្ថបទក្នុង string។

<?php $t = "I love Java"; echo str_replace("Java", "PHP", $t); // I love PHP echo str_replace(" ", "_", "Hello World"); // Hello_World ?>

▶ 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

Answer: str_replace("World","PHP","Hello World") = "Hello PHP"
Category 7: Split & Join

🔗 explode() / implode()

Concept: explode() បំបែក string ទៅជា array។ implode() ភ្ជាប់ array ត្រឡប់ទៅ string វិញ។

<?php $csv = "apple,banana,cherry"; $arr = explode(",", $csv); print_r($arr); // ["apple","banana","cherry"] $joined = implode(" - ", $arr); echo $joined; // apple - banana - cherry ?>

▶ Output:

explode: ["apple", "banana", "cherry"]
implode: apple - banana - cherry

📝 Practice:

តើ explode(" ", "I love PHP") ត្រឡប់ array អ្វី?

Hint: បំបែកដោយ space

Answer: explode(" ", "I love PHP") = ["I", "love", "PHP"]

Interactive🎮 String Playground

Part 2🕐 Date & Time ក្នុង PHP

PHP មាន functions ជាច្រើនសម្រាប់ធ្វើការជាមួយ date និង time។

date() Function

📅 date() - Format Date/Time

Concept: date(\$format) បង្ហាញកាលបរិច្ឆេទនិងម៉ោងតាម format ដែលគែកំណត់។

<?php echo date("Y-m-d"); // 2026-02-12 echo date("d/m/Y"); // 12/02/2026 echo date("H:i:s"); // 14:30:45 echo date("l, d F Y"); // Thursday, 12 February 2026 echo date("d/m/Y H:i"); // 12/02/2026 14:30 ?>

▶ 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 ខ្ទង់

Answer: date("Y") = "2026"
time() & Timestamp

⏰ time() - Unix Timestamp

Concept: time() ត្រឡប់ Unix timestamp (ចំនួនវិនាទីពី 1 Jan 1970)។ ប្រើ date() ដើម្បីបម្លែងវា។

<?php $ts = time(); echo $ts; // 1771052400 echo date("Y-m-d", $ts); // Convert timestamp to 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

Answer: Timestamp = seconds since 1 Jan 1970. date("Y-m-d", $timestamp) converts it.
strtotime() - Parse Date String

🔍 strtotime()

Concept: strtotime() បម្លែង date string ទៅជា timestamp។ អាចប្រើភាសាអង់គ្លេសដែរ។

<?php echo strtotime("2026-02-12"); // timestamp echo strtotime("+7 days"); // 7 days from now echo strtotime("next Monday"); // next Monday echo date("Y-m-d", strtotime("+1 month")); // next month ?>

▶ 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 ថ្ងៃពីថ្ងៃនេះ

Answer: date("Y-m-d", strtotime("+30 days")) = "2026-03-14" (approximately)
DateTime (OOP)

📦 DateTime - Object-Oriented

Concept: DateTime class ជាវិធី OOP សម្រាប់ធ្វើការជាមួយ date/time។ អាចបន្ថែម/ដកថ្ងៃបាន។

<?php $now = new DateTime(); echo $now->format("Y-m-d H:i:s"); // Add 7 days $future = new DateTime(); $future->add(new DateInterval("P7D")); echo $future->format("Y-m-d"); // Subtract 30 days $past = new DateTime(); $past->sub(new DateInterval("P30D")); echo $past->format("Y-m-d"); // Difference $d1 = new DateTime("2026-01-01"); $d2 = new DateTime("2026-12-31"); $diff = $d1->diff($d2); echo $diff->days . " days"; ?>

▶ 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

Answer: $d = new DateTime(); $d->add(new DateInterval("P10D")); echo $d->format("Y-m-d");

Part 3🌍 Timezone

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!

<?php // Set timezone date_default_timezone_set("Asia/Phnom_Penh"); echo date("Y-m-d H:i:s"); // Cambodia time date_default_timezone_set("UTC"); echo date("Y-m-d H:i:s"); // UTC time (7 hours behind) date_default_timezone_set("Asia/Tokyo"); echo date("Y-m-d H:i:s"); // Tokyo time (2 hours ahead) ?>

▶ 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

💡 Note: Server timezone may differ from user timezone. Always use date_default_timezone_set() at the top of your script!

📝 Practice:

Cambodia (Phnom Penh) timezone differs from UTC by how many hours?

Answer: Cambodia = UTC+7 (7 hours ahead of UTC)

Reference📊 Format Characters Table

CharacterMeaningExample (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

Answer: 5

សំណួរទី 2: តើ strtoupper("php") = ?

Hint: uppercase

Answer: "PHP"

សំណួរទី 3: តើ ucwords("hello world") = ?

Hint: អក្សរដំបូងនៃពាក្យនីមួយៗធំ

Answer: "Hello World"

សំណួរទី 4: trim() ធ្វើអ្វី? ប្រើនៅពេលណា?

Hint: កាត់ whitespace

Answer: កាត់ whitespace ពីសងខាងនៃ string។ ប្រើសម្រាប់ clean user input។

សំណួរទី 5: សរសេរកូដដើម្បី replace គ្រប់ spaces ដោយ underscores ក្នុង "Hello World PHP"

Hint: str_replace

Answer: str_replace(" ", "_", "Hello World PHP") = "Hello_World_PHP"

សំណួរទី 6: តើ strpos("abcdef", "a") ត្រឡប់អ្វី? ហេតអ្វីត្រូវប្រើ === false?

Hint: position 0 មិនមែន false

Answer: strpos returns 0. 0 == false is true but 0 === false is false. ត្រូវប្រើ === ដើម្បីពិនិត្យ។

សំណួរទី 7: តើ substr("Programming", 0, 7) = ?

Hint: កាត់ 7 អក្សរដំបូង

Answer: "Program"

សំណួរទី 8: តើ explode(",", "a,b,c") return អ្វី?

Hint: split by comma

Answer: ["a", "b", "c"]

សំណួរទី 9: សរសេរកូដ: បម្លែង string " HELLO world " ទៅ "hello world" (កាត់ space + lowercase)

Hint: ប្រើ trim + strtolower

Answer: strtolower(trim(" HELLO world ")) = "hello world"

សំណួរទី 10: តើ implode("-", ["2026","02","12"]) = ?

Hint: join array

Answer: "2026-02-12"

✏️ Practice: Date & Time (8 Items + 2 Mini-Challenges)

សំណួរទី 1: តើ date("Y") ត្រឡប់អ្វី?

Hint: Y = full year

Answer: date("Y") = "2026"

សំណួរទី 2: តើ date("d/m/Y") output format អ្វី?

Hint: d=day, m=month, Y=year

Answer: "12/02/2026" (day/month/year)

សំណួរទី 3: តើ time() ត្រឡប់អ្វី?

Hint: Unix timestamp

Answer: Returns Unix timestamp (seconds since 1 Jan 1970)

សំណួរទី 4: តើបង្ហាញថ្ងៃ 1 ខែបន្តាប់ពីនេះ ដោយប្រើ strtotime សរសេរយ៉ាងម៉េច?

Hint: strtotime("+1 month")

Answer: date("Y-m-d", strtotime("+1 month"))

សំណួរទី 5: Cambodia timezone ជាអ្វី?

Hint: Asia/...

Answer: Asia/Phnom_Penh (UTC+7)

សំណួរទី 6: Format character ដែលបង្ហាញ full month name ជាអ្វី?

Hint: មើល table ខាងលើ

Answer: F (February, March, etc.)

សំណួរទី 7: តើបង្កើត DateTime ដើម្បីគណនាចំនួនថ្ងៃរវាង 2 dates?

Hint: diff() method

Answer: $d1->diff($d2)->days

សំណួរទី 8: តើ h និង H ខុសគ្ណាជាអ្វី ក្នុង date format?

Hint: h=12h, H=24h

Answer: h = 12-hour format (01-12), H = 24-hour format (00-23)

🏆 Mini-Challenges

Mini-Challenge 1

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"
?>

Mini-Challenge 2

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)";