Solved

Parsing a date with PHP

Posted on 2003-11-09
4
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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