Reputation: 3647
I have a problem with verifying dates in PHP 5.2.17. I need the user to be able to enter the six digit date without any punctuation so Jan. 15, 2014 would be 011514. (This is due to input device limitation and for user convenience).
Tested and this is not working:
$testDate = "011514";
$myDate = date('mdy', strtotime($testDate));
echo $myDate;
I would expect that myDate is the same as test date but it's not, it prints out today's date 042314 (April 23, 2014)! PHP doesn't like that there is no punctuation in my original string. If I change $testDate = "01-15-14" then PHP spits out the correct string "011514".
How can I get strtotime() to understand that I want to check dates in 'mdy' format without using date_create_from_format ?
Extra info: PHP version is 5.2.17 so date_create_from_format is NOT able to be used.
Upvotes: 0
Views: 362
Reputation: 219924
There's a limit to what strtotime() can understand. It can't understand everything. That's why they tell us what it does and we, as developers, need to write our code in a way that respects that.
All you need to do in your code is insert slashes and you're all set.
$testDate = "011514";
$date = sprintf('%s/%s/%s',
substr($testDate, 0, 2),
substr($testDate, 2, 2),
substr($testDate, 4, 2)
);
$myDate = date('mdy', strtotime($date));
echo $myDate;
Upvotes: 1
Reputation: 255135
Answering to your only question
How can I get strtotime() to understand that I want to check dates in 'mdy' format without using date_create_from_format ?
To get strtotime()
to understand your format you need to download php sources, patch it so that it supported such a weird format and use your custom build.
Upvotes: 1