webcs
asked on
Parsing a date with PHP
Of course we all know we can get the system date from:
date("m-d-Y H:i:s");
However....assuming that I have a variable called $mydate with this data already in it, I need to seperate the data back out into its companents.
For example if we assume
$mydate equals "2003-11-09 12:56:19"
I would like now to parse out that variable so I can get the year, month, day, hour, minute and second back.
thanks
date("m-d-Y H:i:s");
However....assuming that I have a variable called $mydate with this data already in it, I need to seperate the data back out into its companents.
For example if we assume
$mydate equals "2003-11-09 12:56:19"
I would like now to parse out that variable so I can get the year, month, day, hour, minute and second back.
thanks
or...
<?PHP
$mydate="2003-11-09 12:56:19";
echo 'Year='.date("Y",strtotime ($mydate)) ."<br>\n";
echo 'Month='.date("m",strtotim e($mydate) )."<br>\n" ;
echo 'Day='.date("d",strtotime( $mydate)). "<br>\n";
echo 'Hour='.date("g",strtotime ($mydate)) ."<br>\n";
echo 'Month='.date("i",strtotim e($mydate) )."<br>\n" ;
echo 'Second='.date("s",strtoti me($mydate ))."<br>\n ";
?>
outputs
Year=2003
Month=11
Day=09
Hour=12
Month=56
Second=19
Just another way to do it, does the same thing that Michael701 did.
Alan
<?PHP
$mydate="2003-11-09 12:56:19";
echo 'Year='.date("Y",strtotime
echo 'Month='.date("m",strtotim
echo 'Day='.date("d",strtotime(
echo 'Hour='.date("g",strtotime
echo 'Month='.date("i",strtotim
echo 'Second='.date("s",strtoti
?>
outputs
Year=2003
Month=11
Day=09
Hour=12
Month=56
Second=19
Just another way to do it, does the same thing that Michael701 did.
Alan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great answers...
Note to self, make sure to replace the g: with an H: when doing the parsing to get military time and not go nutty.
Looked for an hour for an error that wasn't there...figures :)
Note to self, make sure to replace the g: with an H: when doing the parsing to get military time and not go nutty.
Looked for an hour for an error that wasn't there...figures :)
$mydate="2003-11-09 12:56:19";
echo 'strtotime()='.strtotime($
$mydate_array = getdate(strtotime($mydate)
echo 'Year='.$mydate_array['yea
echo 'Month='.$mydate_array['mo
echo 'Day='.$mydate_array['mday
?>
outputs
strtotime()=1068404179
Year=2003
Month=11
Day=9