Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.