Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP string processing

Status
Not open for further replies.

taval

Programmer
Jul 19, 2000
192
GB
Hi,

I have some text and I was wondering how I can do some string processing in PHP. I want to be able to get the month and year in separate strings. The date in my db looks like 2002-01-01.

Any help is well appeciated.
 
$date="2002-03-19";
$date_arr=explode("-",$date);
// Now you have:
// $date[0]=2002 ; $date[1]=03 and $date[2]=19

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Oh,

when I do that I get:

echo ($date[0].&quot;<BR>&quot;); // = 2
echo ($date[1].&quot;<BR>&quot;); // = 0
echo ($date[2].&quot;<BR>&quot;); // = 0

for the date 2005-00-01.


heres my code:

--

$date = $row1[&quot;ExDate&quot;];
$date_arr=explode(&quot;-&quot;,$date);
echo ($date[0].&quot;<BR>&quot;);
echo ($date[1].&quot;<BR>&quot;);
echo ($date[2].&quot;<BR>&quot;);

--
 
Sorry ... my fault ... it's date_arr[0],[1] and [2] instead of array ...

sorry. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
thanks,

just one more thing I want to update a combo box to the value of the year for example:


------

// $date_arr[0] = 2005

<select name=&quot;Year&quot;>
<option selected value='<?=$date_arr[0]?>' >2002
<option value=&quot;2003&quot;>2003
<option value=&quot;2004&quot;>2004
<option value=&quot;2005&quot;>2005
<option value=&quot;2006&quot;>2006
<option value=&quot;2007&quot;>2007
<option value=&quot;2008&quot;>2008
<option value=&quot;2009&quot;>2009
<option value=&quot;2010&quot;>2010
<option value=&quot;2011&quot;>2011
<option value=&quot;2012&quot;>2012 </option>
</select>

-----

is this correct? but this doesn't seem to work.
 
No, it doesn't work that way. You need the &quot;selected&quot; keyword to optionally appear anywhere in that option group, not just one line. It's best to generate the combo box by looping:

Code:
<select name=&quot;Year&quot;>
<?php

for($i = 3; $i <= 12; $i++)
{

  $year = 2000 + $i;
  echo &quot;<option &quot;;
  if($date_arr[0] == $year)
    {
      echo &quot;selected &quot;;
    }
  echo &quot;value='$year'>$year \n&quot;;
}
?>
</select>

This would place the &quot;selected&quot; switch at the right location in your option group, whenever $date_arr[0] matches a value in the option group. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-unknown F student
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top