Skip to content

Instantly share code, notes, and snippets.

@Dinir
Last active September 5, 2017 07:47
Show Gist options
  • Save Dinir/eb4100bc9565d5dcd4d93a518f9ce100 to your computer and use it in GitHub Desktop.
Save Dinir/eb4100bc9565d5dcd4d93a518f9ce100 to your computer and use it in GitHub Desktop.
format 'YYYY-MM-DD' for other languages
<?php
/**
* 날짜 문자열을 다른 언어의 표기 방식에 따라 바꾸어서 반환합니다.
*
* @param string $date_string 'YYYY-MM-DD' 형식으로 표기한 날짜입니다.
* @param string $language 날짜를 표시하려는 언어입니다. (ISO 639 언어 코드)[_(ISO 3166 국가 코드)] 형식을 따릅니다.
*
* @return string 바꾸어진 날짜 문자열입니다.
*/
function format_date(string $date_string, string $language): string {
// expected input of $date_string : '2017-09-05'
$year = substr($date_string, 0, 4);
$month = substr($date_string, 5, 2);
$day = substr($date_string, 8, 2);
$date = '';
switch($language) {
case 'en_US': // expected output : 'September 5th, 2017'
//*
$months = [
's' => [
'Jan', 'Feb', 'Mar',
'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'
],
'l' => [
'January', 'February', 'March',
'April', 'May', 'June',
'July', 'August', 'September',
'October', 'November', 'December'
]
];
$ordinal_suffix = [
'th', 'st', 'nd', 'rd'
];
$month = $months['l'][(int)$month - 1];
$day = (int)$day;
$day .= array_key_exists($day % 10, $ordinal_suffix) ?
$ordinal_suffix[$day % 10] : $ordinal_suffix[0];
$date = "$month $day, $year";
unset($months, $ordinal_suffix);
/*/
$date = "$month/$day/$year";
//*/
break;
}
unset($year, $month, $day);
return $date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment