Strings and dates

Clean up text, format money, handle Tamil text and work with dates.

PHP
<?php
$name = "  kavin kumar  ";
$clean = ucwords(trim($name));
echo $clean . "\n";
echo strlen($clean) . "\n";
echo str_contains($clean, "Kumar") ? "yes\n" : "no\n";
echo str_replace("Kumar", "K", $clean) . "\n";
echo substr($clean, 0, 5) . "\n";

$fee = 12500.5;
echo number_format($fee, 2) . "\n";
printf("%s owes Rs %.2f\n", $clean, $fee);
Output
Kavin Kumar
11
yes
Kavin K
Kavin
12,500.50
Kavin Kumar owes Rs 12500.50

strlen counts bytes, not letters. Tamil text such as "வணக்கம்" takes 21 bytes, so strlen gives 21 while mb_strlen gives 7. Use the mb_ functions, such as mb_strlen and mb_substr, for any text a user types.

Dates

PHP
<?php
date_default_timezone_set("Asia/Kolkata");

$joined = new DateTimeImmutable("2026-06-15");
$ends = $joined->modify("+90 days");
echo $ends->format("d M Y") . "\n";

$days = $joined->diff(new DateTimeImmutable("2026-11-03"))->days;
echo "$days days\n";
Output
13 Sep 2026
141 days

Set the time zone once, at the top of the app or in php.ini, or times can be off by five and a half hours. DateTimeImmutable returns a new object from modify, so the original date never changes by accident.

← ArraysForms and output safety →