Dates and Regular Expressions
date() formats a timestamp and DateTime objects handle dates with modify and diff. preg_match, preg_match_all and preg_replace use regular expressions written between delimiters such as /.../.
Example
$d = new DateTime("2026-03-15");
echo $d->format("d M Y"), "\n";
$d->modify("+30 days");
echo $d->format("Y-m-d"), "\n";
$diff = (new DateTime("2026-01-01"))->diff(new DateTime("2026-01-31"));
echo $diff->days, "\n";
preg_match_all("/\d+/", "Order 12 and 345", $m);
print_r($m[0]);
echo preg_replace("/\s+/", " ", "too many spaces"), "\n";Result
15 Mar 2026
2026-04-14
30
Array
(
[0] => 12
[1] => 345
)
too many spacesExercise
Find all regex matches.
("/\d+/", $text, $m);Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.