Home > Enterprise >  How can I get selected number only in string PHP
How can I get selected number only in string PHP

Time:05-15

how can i echo DAY of the date only?

$date = Saturday, 3 September 2022;

if ($date == '3') {
echo '03';
}

CodePudding user response:

Assuming $date is or has been converted to a string

$date = 'Saturday, 3 September 2022';
$explodedDate = explode(' ', $date);

if ($explodedDate[1] == '3') {
echo '03';
}

CodePudding user response:

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');
  •  Tags:  
  • php
  • Related