PHP在字符串中添加斜杠

时间:2013-06-21 13:17:16

标签: php string date substring

我得到了这个变量:

$payments['date'] = 21062013

问题:

它是一个日期,所以我想在这个字符串中添加斜杠。

我是怎么做的:

echo $paymentdate = substr($payments['date'],2)+/+substr($payments['date'],4)+/substr($payments['date'],8);

它不起作用,有人能帮忙吗?

我的完整代码:

$i = 0;
foreach($SQL->query('SELECT id, form, email, value, data, date, compensated from payments where compensated="N"') as $payments)
{
 $i++;
 $paymentdate = substr($payments['date'], 0, 2)."/".substr($payments['date'], 2, 2)."/".substr($payments['date'], 2, 8);
 echo '<tr bgcolor="' . (is_int($i / 2) ? $config['site']['darkborder'] : $config['site']['lightborder']) . '">  
  <td>' . $payments['id'] . '</td>
  <td>' . $payments['form'] . '</td>
  <td>' . $payments['email'] . '</td>
  <td>' . $payments['value'] . '</td>
  <td>' . $paymentdate .'</td>
  <td>' . $payments['data'] . '</td>
  <td>' . $payments['compensated'] . '</td>
  <td>a</td>
 </tr>';
}

4 个答案:

答案 0 :(得分:2)

使用strtotime()功能更改其显示方式。

<?php echo date("m/d/y", strtotime("20130621")); ?>

答案 1 :(得分:1)

这对你有用;

echo $paymentdate = substr($payments['date'], 0, 2)."/".substr($payments['date'], 2, 2)."/".substr($payments['date'], 4, 4);

答案 2 :(得分:1)

请:

substr($payments['date'], 0, 2) . '/' . substr($payments['date'], 2, 2) . '/' . substr($payments['date'], 4, 4);

答案 3 :(得分:0)

function convertDate($date, $format = 1)
{
    $newDate = "";

   //Convert a string to an array
   $num = str_split($date);

   switch(format)
   {
     case 1:
        $newDate = $num[0].$num[1]."/".$num[2].$num[3]."/".$num[4].$num[5].$num[6]$num[7] ;
        Break;

        // .... 
   }

 return $newDate;
}