dongtun1683
2017-03-27 07:09
浏览 65
已采纳

如何在PHP中将日期从yyyy-mm-dd转换为dd-mm-yyyy? [重复]

This question already has an answer here:

I am storing date in yyyy-mm-dd in database. Now i want to display that date in frontend but while displaying date it should be in dd-mm-yyyy format. How to display it in this format please help <?php echo $data2['emp_dob'];?> . I dont want to change my current format of storing date in yyyy-mm-dd. please help me to display it in dd-mm-yyyy format only

</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 在PHP中将一种日期格式转换为另一种格式 15 answers < / span>

    我将日期存储在数据库中的yyyy-mm-dd中。 现在我想在前端显示该日期,但在显示日期时,它应该是dd-mm-yyyy格式。 如何以这种格式显示它请帮助&lt;?php echo $ data2 ['emp_dob'];?&gt; 。 我不想在yyyy-mm-dd中更改我当前存储日期的格式。 请帮我以dd-mm-yyyy格式显示它

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dongya4335 2017-03-27 07:16
    已采纳

    use this function, which uses inbuilt function 'substr(str,start,length)'.

    function getFormatDate($date){
        $date = substr($date,8,2).'-'.substr($date,5,2).'-'.substr($date,0,4);
        return $date;
    }
    
    getFormatDate($data2['emp_dob']);
    
    已采纳该答案
    打赏 评论
  • doumi5223 2017-03-27 07:13
    date('d-m-Y',strtotime($Your_date));
    
    打赏 评论
  • douxigai8757 2017-03-27 07:13
    if (preg_match("/([0-9]{4})\-([0-9]{2})\-([0-9]{2})/", $data2['emp_dob'], $rg))
      $data2['emp_dob'] = $rg[3]."-".$rg[2]."-".$rg[1]
    
    打赏 评论
  • doudouba4520 2017-03-27 07:13

    I don't think it's strictly correct to say that you're storing dates in your MySQL database as yyyy-mm-dd. The internal representation may be something very different than this, but in any case your real question is how to format the date in a certain way. One option is to handle this in your actual MySQL query using DATE_FORMAT(), e.g.

    SELECT DATE_FORMAT(date_col, '%d-%m-%Y')
    FROM yourTable
    
    打赏 评论
  • dongzaheng4449 2017-03-27 07:14
    <?php
      $ddmmyyyy = date("d-m-Y", strtotime($data2['emp_dob']));
    ?>
    
    打赏 评论
  • douluo3256 2017-03-27 07:14

    <?php echo date("d-m-Y",strtotime($data2['emp_dob']));?>

    </div>
    
    打赏 评论

相关推荐 更多相似问题