0

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. :)

0

2 Answers 2

4

Use explode function

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

Refer following link for more information

Click Here

Sign up to request clarification or add additional context in comments.

Comments

3

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)

4 Comments

remember: simplest method will be selected but, regex is preferable. :) i want to explore more on regex.
can we use list to simplify the code?
@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.
its not work. Parse error: syntax error, unexpected ')', expecting '='

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.