If I've got a date string:
$date = "08/20/2009";
And I want to separate each part of the date:
$m = "08";
$d = "20";
$y = "2009";
How would I do so?
Is there a special date function I should be using? Or something else?
Thanks!
If I've got a date string:
$date = "08/20/2009";
And I want to separate each part of the date:
$m = "08";
$d = "20";
$y = "2009";
How would I do so?
Is there a special date function I should be using? Or something else?
Thanks!
explode
will do the trick for that:
$pieces = explode("/", $date);
$d = $pieces[1];
$m = $pieces[0];
$y = $pieces[2];
Alternatively, you could do it in one line (see comments - thanks Lucky):
list($m, $d, $y) = explode("/", $date);
One way would be to do this:
$time = strtotime($date);
$m = date('m', $time);
$d = date('d', $time);
$y = date('Y', $time);
Is it always like that? Or will it be in any sort of file format?
Try strtotime.
Something like:
if(($iTime = strtotime($strDate))!==false)
{
echo date('m', $iTime);
echo date('d', $iTime);
echo date('y', $iTime);
}
Dominic's answer is good, but IF the date is ALWAYS in the same format you could use this:
$m = substr($date,0,2);
$d = substr($date,3,2);
$y = substr($date,-4);
and not have to use an array or explode
Bill H
how about this:
list($m, $d, $y) = explode("/", $date);
A quick one liner.