Solved

Parsing a date with PHP

Posted on 2003-11-09
4
266 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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