文字列を時間に変換しようとして問題が発生しています。
私のコードは次のとおりです。
$time = strtotime("14 November, 2013 2:30 AM");
echo $time ."<br />";
echo date("m/d/Y", $time);
strtotimeは魔法ではないことを知っています。受け入れ可能な日付/時刻形式を確認しましたが、最初に時刻に変換せずに文字列を別の文字列に変換する方法がわかりません。
これを達成する最も簡単な方法は何ですか?
DateTime::createFromFormat を見て、作成された DateTime インスタンスで format を呼び出します。
何かのようなもの:
$yourTimeString = '14 November, 2013 2:30 AM';
$date = DateTime::createFromFormat('d F, Y h:i A', $yourTimeString);
echo $date->format('m/d/Y');
explode1 つの方法は、 と を使用して文字列を書き換えることlistです。
<?php
// here we assume "day month, year time AMPM"
$date = "14 November, 2013 2:30 AM";
// assign a variable to each part of the string
list($day,$month,$year,$time,$ampm) = explode(" ",$date);
// remove the commas at the end of the month
$month = str_replace(',','',$month);
// Now we rewrite the strtotime string
$time = strtotime($month . " " . $day . ", " . $year . " " . $time . " " . $ampm);
echo $time ."<br />";
echo date("m/d/Y", $time);