'How can I get selected number only in string PHP
how can i echo DAY of the date only?
$date = Saturday, 3 September 2022;
if ($date == '3') {
echo '03';
}
Solution 1:[1]
Assuming $date is or has been converted to a string
$date = 'Saturday, 3 September 2022';
$explodedDate = explode(' ', $date);
if ($explodedDate[1] == '3') {
echo '03';
}
Solution 2:[2]
I think, converting the date to a timestamp is more save than jasond1284's answer.
echo date('d', strtotime($date));
or if you want to use the object oriented DateTime class:
$DT = new DateTime($date);
echo $DT->format('d');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jasond1284 |
| Solution 2 | Aranxo |
