@@ -292,14 +292,22 @@ public static function convertToSalesforceFormat($input)
292292
293293 $ input = trim ($ input );
294294
295- // ------------------------------------------------------------
296- // 1) Handle UNIX timestamps (10 or 13 digits)
297- // ------------------------------------------------------------
298- if (preg_match ('/^\d{10}$/ ' , $ input )) {
299- return gmdate ('Y-m-d\TH:i:s\Z ' , (int ) $ input );
295+ if (empty ($ input )) {
296+ return $ input ;
300297 }
301- if (preg_match ('/^\d{13}$/ ' , $ input )) {
302- return gmdate ('Y-m-d\TH:i:s\Z ' , (int ) ($ input / 1000 ));
298+
299+ if (is_numeric ($ input )) {
300+ return $ input ;
301+
302+ // ------------------------------------------------------------
303+ // 1) Handle UNIX timestamps (10 or 13 digits)
304+ // ------------------------------------------------------------
305+ if (\strlen ($ input ) === 10 && preg_match ('/^\d{10}$/ ' , $ input )) {
306+ return gmdate ('Y-m-d\TH:i:s\Z ' , (int ) $ input );
307+ }
308+ if (\strlen ($ input ) === 13 && preg_match ('/^\d{13}$/ ' , $ input )) {
309+ return gmdate ('Y-m-d\TH:i:s\Z ' , (int ) ($ input / 1000 ));
310+ }
303311 }
304312
305313 // ------------------------------------------------------------
@@ -356,7 +364,7 @@ public static function convertToSalesforceFormat($input)
356364 // ------------------------------------------------------------
357365 // 7) Compact numeric formats (01022025, 20250201, 250201, 010225)
358366 // ------------------------------------------------------------
359- if (preg_match ('/^\d{8}$/ ' , $ clean )) {
367+ if (\strlen ( $ clean ) === 8 && preg_match ('/^\d{8}$/ ' , $ clean )) {
360368 // YYYYMMDD or DDMMYYYY or MMDDYYYY → try multiple interpretations
361369 $ candidates = [
362370 substr ($ clean , 0 , 4 ) . '- ' . substr ($ clean , 4 , 2 ) . '- ' . substr ($ clean , 6 , 2 ), // YMD
@@ -369,7 +377,7 @@ public static function convertToSalesforceFormat($input)
369377 }
370378 }
371379
372- if (preg_match ('/^\d{6}$/ ' , $ clean )) {
380+ if (\strlen ( $ clean ) === 6 && preg_match ('/^\d{6}$/ ' , $ clean )) {
373381 // DDMMYY / YYMMDD / MMDDYY
374382 $ yy = \intval (substr ($ clean , -2 ));
375383
@@ -399,15 +407,7 @@ public static function convertToSalesforceFormat($input)
399407 // ------------------------------------------------------------
400408 // 8) Try direct DateTime parsing for most formats
401409 // ------------------------------------------------------------
402- $ tryParse = function ($ value ) {
403- try {
404- return new DateTimeImmutable ($ value );
405- } catch (Throwable $ e ) {
406- return ;
407- }
408- };
409-
410- $ dt = $ tryParse ($ clean );
410+ $ dt = self ::tryParseDateTimeImmutable ($ clean );
411411
412412 if ($ dt instanceof DateTimeImmutable) {
413413 // Detect if datetime or pure date
@@ -440,4 +440,13 @@ public static function convertToSalesforceFormat($input)
440440 return $ input ;
441441 }
442442 }
443+
444+ private static function tryParseDateTimeImmutable ($ value )
445+ {
446+ try {
447+ return new DateTimeImmutable ($ value );
448+ } catch (Throwable $ e ) {
449+ return ;
450+ }
451+ }
443452}
0 commit comments