2013-12-13 14 views
17

残念ながらDateTime()はこのプロジェクトが実行されているサーバーとしてPHP v.5.2を使用できません。

問題の行:

$aptnDate2 = date('Y-m-d', $_POST['nextAppointmentDate']); 

は、次のエラーがスローされます。

Notice: A non well formed numeric value encountered 

ので、私はそれがうまくフォーマットされています確認するために、ダンプVAR ..

var_dump($_POST['nextAppointmentDate']); 

string(10) "12-16-2013" 

php docs stateことそれは文字列ではなくタイムスタンプをとる。私が行うときには:

date('Y-m-d', strtotime($_POST['nextAppointmentDate'])); 

、その後var_dump結果を、私はこれを取得:

string(10) "1969-12-31" 

なぜ私はこの日付値とのstrtotime(と日付をフォーマットすることはできませんか)?

ありがとうございました!

答えて

43

:あなたの日付文字列で

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

、あなたは12-16-2013を持っています。 16は有効な月ではないため、strtotime()falseを返します。

あなたがDateTimeクラスを使用することはできませんので、手動で-strtotime()が理解できる形式に日付文字列を変換するstr_replace()を使用して/で置き換えることができます:

$date = '2-16-2013'; 
echo date('Y-m-d', strtotime(str_replace('-','/', $date))); // => 2013-02-16 
+0

乾杯メイト、それはそれでした。私はダッシュが受け入れられると主張することができました、おそらくそれはちょうど 'DateTime()'でしょうか?ご協力ありがとうございました。 :) – Prefix

+0

@Prefix:誤ってフォーマットされた日付文字列を 'DateTime'に渡すと、(通常は)例外をスローします。受け入れられた日付形式のリストについては、[the documentation](http://www.php.net/manual/en/datetime.formats.date.php)を参照してください。 DateTimeを使用している場合は、** DateTime :: createFromFormat()を**移動する方法です:https://eval.in/79189 –

-4

date()関数の第2引数は、日付文字列ではなく、unixタイムスタンプです。 explode()を使って日付文字列を分割し、再度組み合わせてください。 strtotimeはそのような形式の日付を期待していません。 strtotime()のドキュメントから

関連する問題