views:

68

answers:

2

i have $date variable 2009-04-29 which is Y-m-d

anybody can give idea how to extract into $d, $m, $y using simplest method as possible? regex is preferable. any more suggestion with simple method will be chosen. :)

+3  A: 

Use explode function

list ( $y, $m , $d ) = explode( '-' , $date ) ; 

Refer following link for more information

Click Here

pavun_cool
nice idea pavun_cool.. :)
apis17
+1  A: 

You could easily avoid regex by using

$y = substr($date, 0,4);
$m = substr($date, 5,2);
$d = substr($date, -2);

Edit: If you really want regex, you will have one =)

Try

preg_match('/(\d{4})-(\d{2})-(\d{2})/',  $date, $matches)
$y = $matches[1];
$m = $matches[2];
$d = $matches[3];

(I do not know if the backslashes in the expression need to be escaped, sorry)

Jens
remember: simplest method will be selected but, regex is preferable. :) i want to explore more on regex.
apis17
@apis17: Here you are. =)
Jens
thank you Jens. :)
apis17
can we use list to simplify the code?
apis17
@apsi17: Dunno. Just try `preg_match('/(\d{4})-(\d{2})-(\d{2})/', $date, list(, $y, $m, $d))` and report back. I have no php interpreter available to me at the moment.
Jens
its not work. `Parse error: syntax error, unexpected ')', expecting '=' `
apis17