PHP Dateformat not working as expected

Matt Lachman

Given the following PHP code:

$thestring = "Saturday - 05/10/2014 at 10:00 am";

echo $thestring."\n";

$thestring = str_replace("at ", "", $thestring);

echo $thestring."\n";

$thestring = substr($thestring, strpos($thestring, " - ")+3);

echo $thestring."\n";

$thedate = date_create_from_format("m/d/Y h:m a", $thestring);

echo $thedate->format("Ymd")."\n";
echo $thedate->format("Y")."\n";
echo $thedate->format("m")."\n";
echo $thedate->format("d")."\n";

Why am I getting the output below? The month and year are off and I don't see a logical explanation for it.

Saturday - 05/10/2014 at 10:00 am
Saturday - 05/10/2014 10:00 am
05/10/2014 10:00 am
20131210
2013
12
10
John Conde

m is the modifier for months. i is the modifier for minutes. Getting this wrong will obviously cause issues. Change:

$thedate = date_create_from_format("m/d/Y h:m a", $thestring);

to:

$thedate = date_create_from_format("m/d/Y h:i a", $thestring);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

String Replace in php not working as expected

分類Dev

AJAX call is not working as expected

分類Dev

jQuery .when().then() not working as expected

分類Dev

.vimrc file not working as expected

分類Dev

*ngIf not working as expected with observable

分類Dev

ItemIsAutoTristate flag not working as expected

分類Dev

KeyboardAvoidingView not working as expected on IOS

分類Dev

PowerShell variables not working as expected

分類Dev

XPath logical 'and' not working as expected?

分類Dev

CancellationTokenSource not working as expected on a TaskCompletionSource

分類Dev

string formatting not working as expected

分類Dev

Why is .on() Not working as expected in jquery?

分類Dev

Tuples in Scala not working as expected

分類Dev

strpos() not working as expected

分類Dev

Update not working as expected

分類Dev

ifelse not working as expected in R

分類Dev

$("#form").submit(); is not working as expected

分類Dev

Prototype is not working as expected

分類Dev

LIKE and Equals not working as expected

分類Dev

Regex is not working as expected in javascript

分類Dev

angularjs service not working as expected

分類Dev

NSTask not working as expected / hoped for

分類Dev

Routing not working as expected in CodeIgniter

分類Dev

mhddfs not working as expected

分類Dev

Python: If...and... not working as expected

分類Dev

timeout for ngShow not working as expected

分類Dev

extglob negation not working as expected

分類Dev

LIKE query not working as expected

分類Dev

boostrap clearfix not working as expected