Solved

Parsing a date with PHP

Posted on 2003-11-09
4
259 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:webcs
4 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 9710874
<?PHP
$mydate="2003-11-09 12:56:19";

echo 'strtotime()='.strtotime($mydate)."<br>\n";

$mydate_array = getdate(strtotime($mydate));

echo 'Year='.$mydate_array['year']."<br>\n";
echo 'Month='.$mydate_array['mon']."<br>\n";
echo 'Day='.$mydate_array['mday']."<br>\n";
?>

outputs

strtotime()=1068404179
Year=2003
Month=11
Day=9
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 9717269
or...

<?PHP
$mydate="2003-11-09 12:56:19";
echo 'Year='.date("Y",strtotime($mydate))."<br>\n";
echo 'Month='.date("m",strtotime($mydate))."<br>\n";
echo 'Day='.date("d",strtotime($mydate))."<br>\n";
echo 'Hour='.date("g",strtotime($mydate))."<br>\n";
echo 'Month='.date("i",strtotime($mydate))."<br>\n";
echo 'Second='.date("s",strtotime($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

0
 
LVL 1

Accepted Solution

by:
yahelb earned 125 total points
ID: 9717373
or ...
you can do that in one line without calling strtotime 6 times!

<?php
$mydate="2003-11-09 12:56:19";

list($year, $month, $day, $hour, $min, $sec) = split(";", date("Y;m;d;g;i;s", strtotime($mydate)));

echo $year."<br>";
echo $month."<br>";
echo $day."<br>";
echo $hour."<br>";
echo $min."<br>";
echo $sec."<br>";
?>
0
 
LVL 2

Author Comment

by:webcs
ID: 9725421
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 :)

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now