diff --git a/solution/1500-1599/1507.Reformat Date/README.md b/solution/1500-1599/1507.Reformat Date/README.md index 1ff37102fb5a1..7faa54f6e62b5 100644 --- a/solution/1500-1599/1507.Reformat Date/README.md +++ b/solution/1500-1599/1507.Reformat Date/README.md @@ -123,6 +123,26 @@ func reformatDate(date string) string { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $date + * @return String + */ + function reformatDate($date) { + $arr = explode(" ", $date); + $months = array("Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12"); + $year = $arr[2]; + $month = $months[$arr[1]]; + $day = intval($arr[0]); + if ($day > 0 && $day < 10) $day = "0".$day; + return $year."-".$month."-".$day; + } +} +``` + ### **...** ``` diff --git a/solution/1500-1599/1507.Reformat Date/README_EN.md b/solution/1500-1599/1507.Reformat Date/README_EN.md index df55f61736728..e17e8d3327f15 100644 --- a/solution/1500-1599/1507.Reformat Date/README_EN.md +++ b/solution/1500-1599/1507.Reformat Date/README_EN.md @@ -110,6 +110,26 @@ func reformatDate(date string) string { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $date + * @return String + */ + function reformatDate($date) { + $arr = explode(" ", $date); + $months = array("Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12"); + $year = $arr[2]; + $month = $months[$arr[1]]; + $day = intval($arr[0]); + if ($day > 0 && $day < 10) $day = "0".$day; + return $year."-".$month."-".$day; + } +} +``` + ### **...** ``` diff --git a/solution/1500-1599/1507.Reformat Date/Solution.php b/solution/1500-1599/1507.Reformat Date/Solution.php new file mode 100644 index 0000000000000..635729d4fcf51 --- /dev/null +++ b/solution/1500-1599/1507.Reformat Date/Solution.php @@ -0,0 +1,15 @@ +class Solution { + /** + * @param String $date + * @return String + */ + function reformatDate($date) { + $arr = explode(" ", $date); + $months = array("Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12"); + $year = $arr[2]; + $month = $months[$arr[1]]; + $day = intval($arr[0]); + if ($day > 0 && $day < 10) $day = "0".$day; + return $year."-".$month."-".$day; + } +} \ No newline at end of file