Solved

Parsing a date with PHP

Posted on 2003-11-09
4
249 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
Comment Utility
<?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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

743 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

12 Experts available now in Live!

Get 1:1 Help Now