PHP将MM / DD / YYYY字符串转换为MMDDYYYY [复制]
This question already has an answer here:
- Convert one date format into another in PHP 15 answers
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>
此问题已经存在 这里有一个答案: p>
-
在PHP中将一种日期格式转换为另一种格式
15 answers
li>
ul>
div>
我有一个表示日期的字符串。 它的格式是
MM / DD / YYYY code>。
我需要通过API将它提交给需要MMDDYYYY code>的服务。
我是否应该为重载此字符串而烦恼 在某个日期类和“导出”为MMDDYYY code>或只是删除“/”子字符串,无论我在哪里找到它? p>
任何其他整齐的方式 这样做我不知道? strong> p> div>
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;
?>
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);