I have a string date like this:
$date = "23/12/2011";//format: dd/mm/yyyy.
I need each part of this date in its own variable, so that $d=="23", $m=="12", and $y=="2011".
Any suggestions on how to do this would be appreciated...
You could just do:
list($day, $month, $year) = split("[/]", $yourDate);
The regex you're looking for is /(\d+)\/(\d+)\/(\d+)/
and in the preg_match
they would be contained in the $matches array you define in the preg_match
call.
$date = "23/12/2011";
list($d,$m,$y) = explode('/', $date, 3)
Backreferences:
if (preg_match('%(\d{2})/(\d{2})/(\d{4})%', $subject, $regs)) {
$result = $regs[1]; #first two digits are here.. etc..
}
With this solution, each variable is automatically assigned the proper datatype:
sscanf($date, '%d/%d/%d', $d, $m, $y);