开发者

How to separate date in php

I want to be able to separate the birthday from the mysql data into day, year and month. Using the 3 t开发者_StackOverflow中文版extbox in html. How do I separate it? I'm trying to think of what can I do with the code below to show the result that I want:

Here's the html form with the php code:

$idnum = mysql_real_escape_string($_POST['idnum']);

mysql_select_db("school", $con);
  $result = mysql_query("SELECT * FROM student WHERE IDNO='$idnum'");

  $month = mysql_real_escape_string($_POST['mm']);
?>       
<?php while ( $row = mysql_fetch_array($result) ) { ?>
    <tr>
    <td width="30" height="35"><font size="2">Month:</td>
    <td width="30"><input name="mo" type="text" id="mo"  onkeypress="return handleEnter(this, event)" value="<?php  echo $month = explode("-",$row['BIRTHDAY']);?>">

As you can see the column is the mysql database is called BIRTHDAY. With this format:

YYYY-MM-DD

How do I do it. So that the data from the single column will be divided into three parts? Please help thanks,


You can use list and explode to get desired result

 list($year,$month,$day)=explode("-", $row['birthday']);

hence $year contains year, $month contains month and $day contains Day, you can use it like this in your text boxes

<input name="mo" type="text" id="mo" value="<?php echo $month;?>">
<input name="dt" type="text" id="dt" value="<?php echo $day;?>">
<input name="yr" type="text" id="yr" value="<?php echo $year;?>">


$parts = explode('-', '1912-03-22');
$parts = split('-', '1912-03-22'); // could also use split() 
echo 'Year: ' + $parts[0] . '<br />';
echo 'Month: ' + $parts[1] . '<br />'; 
echo 'Day: ' + $parts[2] . '<br />';  


You could use php's explode function to split the date into its separate compoents:

$birthday ="1981-06-22";
list($year, $month, $day) = explode("-", $birthday);
echo "\nDay $day Month $month Year $year";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜