dongyu8694
dongyu8694
2015-05-15 20:44
浏览 50
已采纳

PHP将MM / DD / YYYY字符串转换为MMDDYYYY [复制]

This question already has an answer here:

I have a string representing a date. It's format is MM/DD/YYYY.
I need to submit it via API to a service that requires MMDDYYYY.
Should I bother with overloading this string onto some date class and 'export' as MMDDYYY or just delete the "/" sub-string wherever I find it?

Any other neat way of doing that I wasn't aware of?

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanmoen784988
    duanmoen784988 2015-05-15 20:49
    已采纳

    You can use the DateTime object in PHP (http://php.net/manual/en/class.datetime.php)

    Here is how I would do it:

    <?php
    
    $myDate = '05/15/2015';
    
    $date = DateTime::createFromFormat('m/d/Y H:i:s', "$myDate 00:00:00");
    $newDate = $date->format('mdY');
    
    echo $newDate . PHP_EOL;
    
    ?>
    
    点赞 评论
  • douhuan5073
    douhuan5073 2015-05-15 20:46

    Since you just need to remove the '/' I would do that with the following code, it will eliminate the overhead of parsing the date.

    $newDate =  str_replace ('/', '', $oldDate);
    
    点赞 评论

相关推荐