diff --git a/src/MD_STD.php b/src/MD_STD.php index 5293da4..00b0e05 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -939,12 +939,11 @@ final class MD_STD { */ public static function date_to_int(string $date):int { - // Edge case: -0000-01-01 - if (substr($date, 0, 5) === '-0000') { - return -1 * intval(str_replace("-", "", ltrim($date, "0-"))); - } + $isNegative = substr($date, 0, 1) === '-'; + $parts = explode('-', $date); - return intval(date("Ymd", self::strtotime($date))); + if ($isNegative === true) return -1 * intval(implode($parts)); + else return intval(implode($parts)); } @@ -973,7 +972,7 @@ final class MD_STD { $day = substr($dateStr, -2, 2); $month = substr($dateStr, -4, 2); - $year = substr($dateStr, -8, 4); + $year = substr($dateStr, 0, -4); return match($isNegative) { true => '-', diff --git a/tests/MD_STD_Test.php b/tests/MD_STD_Test.php index 84c6650..754236c 100644 --- a/tests/MD_STD_Test.php +++ b/tests/MD_STD_Test.php @@ -27,8 +27,8 @@ final class MD_STD_Test extends TestCase { ["-0022-01-01", -220101], ["-2022-01-01", -20220101], ["-0001-01-01", -10101], - ["-0000-01-01", -101], ["0000-01-01", 101], + ["100000-01-01", 1000000101], ]; $output = [];