@@ -114,6 +114,8 @@ public static function evaluate(
114114 return self ::sqlInetAton ($ conn , $ scope , $ expr , $ row , $ result );
115115 case 'INET_NTOA ' :
116116 return self ::sqlInetNtoa ($ conn , $ scope , $ expr , $ row , $ result );
117+ case 'STR_TO_DATE ' :
118+ return self ::sqlStrToDate ($ conn , $ scope , $ expr , $ row , $ result );
117119 }
118120
119121 throw new ProcessorException ("Function " . $ expr ->functionName . " not implemented yet " );
@@ -1533,4 +1535,63 @@ private static function getPhpIntervalFromExpression(
15331535 throw new ProcessorException ('MySQL INTERVAL unit ' . $ expr ->unit . ' not supported yet ' );
15341536 }
15351537 }
1538+
1539+ private static function sqlStrToDate (
1540+ FakePdoInterface $ conn ,
1541+ Scope $ scope ,
1542+ FunctionExpression $ expr ,
1543+ array $ row ,
1544+ QueryResult $ result
1545+ ) : string | false {
1546+ $ args = $ expr ->args ;
1547+
1548+ if (\count ($ args ) !== 2 ) {
1549+ throw new ProcessorException ("MySQL DATE_FORMAT() function must be called with one argument " );
1550+ }
1551+
1552+ $ subject = (string ) Evaluator::evaluate ($ conn , $ scope , $ args [0 ], $ row , $ result );
1553+ $ format = (string ) Evaluator::evaluate ($ conn , $ scope , $ args [1 ], $ row , $ result );
1554+
1555+ if (strpos ($ format , '% ' ) === false ) {
1556+ return false ;
1557+ }
1558+
1559+ $ date_format_list = [
1560+ "%b " => "M " , "%c " => "n " , "%d " => "d " , "%D " => "jS " , "%e " => "j " ,
1561+ "%m " => "m " , "%M " => "F " , "%y " => "y " , "%Y " => "Y "
1562+ ];
1563+
1564+ $ time_format_list = [
1565+ "%h " => "h " , "%H " => "H " , "%i " => "i " , "%I " => "h " , "%k " => "G " ,
1566+ "%l " => "g " , "%r " => "h:i:s A " , "%s " => "s " , "%S " => "s " , "%T " => "H:i:s "
1567+ ];
1568+
1569+ $ has_date_format = false ;
1570+ $ has_time_format = false ;
1571+ preg_match_all ("/(?:%[a-zA-Z])/u " , $ format , $ matches );
1572+ foreach ($ matches [0 ] as $ match ) {
1573+ $ has_date_format = $ has_date_format || in_array ($ match , array_keys ($ date_format_list ));
1574+ $ has_time_format = $ has_time_format || in_array ($ match , array_keys ($ time_format_list ));
1575+ }
1576+
1577+
1578+ $ format = \str_replace (
1579+ array_keys ($ date_format_list + $ time_format_list ),
1580+ array_values ($ date_format_list + $ time_format_list ),
1581+ $ format
1582+ );
1583+
1584+ if ($ has_date_format && $ has_time_format ) {
1585+ return \DateTimeImmutable::createFromFormat ($ format , $ subject )->format ('Y-m-d G:i:s ' );
1586+ }
1587+
1588+ if ($ has_date_format ) {
1589+ return \DateTimeImmutable::createFromFormat ($ format , $ subject )->format ('Y-m-d ' );
1590+ }
1591+
1592+ if ($ has_time_format ) {
1593+ return (\DateTimeImmutable::createFromFormat ($ format , $ subject ))->format ('G:i:s ' );
1594+ }
1595+ return false ;
1596+ }
15361597}
0 commit comments