gretorx Posted May 22, 2010 Share Posted May 22, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/202584-date-parsing-problem/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 22, 2010 Share Posted May 22, 2010 Give this a try - <?php $input = $_GET['string']; $dates = explode('; ', $input); foreach ($dates as $values) { echo $values; $parts = preg_split("/[\s,]+/", $values); print_r($parts); echo "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/202584-date-parsing-problem/#findComment-1061966 Share on other sites More sharing options...
gretorx Posted May 22, 2010 Author Share Posted May 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/202584-date-parsing-problem/#findComment-1061985 Share on other sites More sharing options...
gretorx Posted May 22, 2010 Author Share Posted May 22, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/202584-date-parsing-problem/#findComment-1061991 Share on other sites More sharing options...
kenrbnsn Posted May 22, 2010 Share Posted May 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/202584-date-parsing-problem/#findComment-1062037 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.