Jump to content

Date Parsing problem


gretorx

Recommended Posts

I have input that looks like the following: "December 18, 2002; July 25, 1992; March 1, 1999"

 

The script below is supposed to make each date a variable in an array, then separate the month, the day, and the year into separate variables. It does just that, except for some reason, the day variable returns more than just the day. If you use the code below you'll notice that when you try to echo the day, it returns "1,1999" instead of just "1".

 

Am I overlooking something? The solution to this is probably very simple. There's a reason for all of this crazy parsing, so that's why it's being done this way.

 

<?php

$input = $_GET['string'];

$dates = explode('; ', $input);


foreach ($dates as $values)
   {
   $mon_pos = strpos($values, " ");
   $month = substr($values, 0, $mon_pos);

   $day_pos = strrpos($values, ",");
   $day = substr($values, $mon_pos, $day_pos);

   $year_pos = strrpos($values, " ");
   $year = substr($values, $day_pos +2, strlen($values));

   echo $mon . " " . $day . " " . $year . "<br>\n";
   }



?>

<html>
<body>
<form method="get" action="SOP9.php">
<input type="text" name="string" value="">
<input type="submit" value="Submit">
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/202584-date-parsing-problem/
Share on other sites

I could do that, but the way I've done it is pretty much the only way I can think of that will allow me to easily manipulate the month variable the way that I want to.  I just don't know why the day variable isn't turning out right... It's parsed pretty much the same way the other variables are, so... it should work.

Well I coded the solution, not that anyone here would really be interested, but here it is:

 

<?php

$input = $_GET['string'];

$dates = explode('; ', $input);


foreach ($dates as $values)
   {
   $mon_pos = strpos($values, " ");
   $month = substr($values, 0, $mon_pos);

   $year_pos = strrpos($values, " ");
   $year = substr($values, $day_pos +2, strlen($values));

   $day_pos = strlen($values) - $year_pos;
   $day = substr($values, $mon_pos, $day_pos -2);
   $day2 = str_replace(',','',$day);

   echo $day2 . "<br>\n";
   echo $mon_pos . " " . $day_pos . "<br>\n";
   }



?>

<html>
<body>
<form method="get" action="SOP9.php">
<input type="text" name="string" value="">
<input type="submit" value="Submit">
</form>
</body>
</html>

Try this instead:

<?php
$str = "December 18, 2002; July 25, 1992; March 1, 1999";
$dates = explode(';',$str);
foreach ($dates as $date) {
list($mon,$day,$year) = explode(' ',date('F j Y',strtotime($date)));
   echo "$mon $day $year<br>\n";
}
?>

or

<?php
$str = "December 18, 2002; July 25, 1992; March 1, 1999";
$dates = explode(';',$str);
foreach ($dates as $date) {
   echo date('F j Y',strtotime($date));
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.