I know this has been beaten to death on here, and I've read through all the posts over the years, but cannot find a solution. I'm trying to rearrange a date and have used various methods, but keep getting this error.
$dateMDY = trim($dateTime2[1]);
$dateMDYp = preg_replace("~(.*?)/(.*?)/(.*?)~", "$1-$2-$3", $dateMDY);
echo "dateMDY: '".$dateMDY."'<br /><br /><br />\n";
echo "dateMDYp: '".$dateMDYp."'<br /><br /><br />\n";
$dt = DateTime::createFromFormat('n/j/y', '12/2/17');
echo "dt: '".$dt->format('Y-m-d')."'<br />\n";
$dt2 = DateTime::createFromFormat('n-j-y', '12-2-17');
echo "dt2: '".$dt2->format('Y-m-d')."'<br />\n";
$dt3 = DateTime::createFromFormat('n/j/y', $dateMDY);
echo "dt3: '".$dt3->format('Y-m-d')."'<br />\n";
$dt4 = DateTime::createFromFormat('n-j-y', $dateMDYp);
echo "dt4: '".$dt4->format('Y-m-d')."'<br />\n";
This throws the following:
dateMDY: '12/2/17'
dateMDYp: '12-2-17'
dt: '2017-12-02'
dt2: '2017-12-02'
( ! ) Fatal error: Uncaught Error: Call to a member function format() on boolean
I must be missing something very simple, as DateTime:: has to be sending false for some reason, yet the format that is echoed when I call the variable shows the correct number.
PHP v.7.1.0
Ok. I've solved the issue. I started var_dump-ing all the variables until I noticed something...
C:\wamp64\www\a\datetime.php:47:string '12/2/17</URD>' (length=13)
While print and echo show only '12/2/17', var_dump shows trailing data "</URD>" which is a tag I have in the database table I'm calling from. I'm not sure why the complete data isn't being shown on print or echo, but a simple str_replace to my $dateMDY variable removed the unseen </URD> and now prints the correct format.
Hopefully this can help someone in the future who may be scratching their heads at unseen data being attached to their variables!
Thanks to @Mehrdad Dastgir for illuminating an idea for me.