Reputation: 4023
Running PHP 5.3.28
I have a particular date getting returned as a string that strtotime is choking on, so I thought to try out DateTime::createFromFormat(), however despite best efforts getting errors.
<?php
$freshdate = '09/07/2015 (Mon)';
$date = DateTime::createFromFormat("m/d/Y (D)", $freshdate);
if (!$date) {
var_dump('error', DateTime::getLastErrors());
}
Result is:
array (size=4)
'warning_count' => int 0
'warnings' => array (size=0)
'error_count' => int 2
'errors' => array (size=2)
12 => string 'A textual day could not be found' (length=32)
16 => string 'Data missing' (length=12)
Upvotes: 2
Views: 1310
Reputation: 1022
This is bug in PHP#65554:
createFromFormat fails when in the format D or l is followed by separators that are not space or comma.
How to fix? Change your format or upgrade your PHP version.
Upvotes: 4
Reputation: 212522
From the changelog for 5.4.20: - Fixed bug #65554 (createFromFormat broken when weekday name is followed by some delimiters)
So the problem is the trailing )
after the day name
You can workround it by trimming that trailing )
:
$freshdate = '09/07/2015 (Mon)';
$date = DateTime::createFromFormat("m/d/Y (D", rtrim($freshdate, ')'));
if (!$date) {
var_dump('error', DateTime::getLastErrors());
}
Upvotes: 3
Reputation: 119
Use this-
<?php
$freshdate = '09/07/2015 (Mon)';
$date = DateTime::createFromFormat("m/d/Y (D)", $freshdate);
echo $date->format('m/d/Y (D)');
Upvotes: 2
Reputation: 2025
This should work, convert it to a unix timestamp:
$freshdate = explode('/', '09/07/2015 (Mon)');
$year = explode(' ', $freshdate[2]);
$unix_date = mktime(0,0,0,$freshdate[0],$freshdate[1],$year[0]);
Might not be the best answer, but its definitely AN answer :)
Upvotes: 0