Convert between English and Arabic Gregorian Months

This is a simple string replacement. It doesn’t convert between Gregorian and another calendar, it just replaces the Gregorian month names written in Arabic with the Gregorian month names written in Arabic, and vice versa. It may be useful when dealing with dates saved as strings.

/**
 * Converts string containing Arabic months into string with English months  * e.g. 1 يناير 2018 
 * => 1 January 2018
 *
 * @param string $string containing an Arabic date
 * @return string 
 */
 
function convert_arabic_months_to_english( $string ) {
	$english_months = array('January','February','March','April','May','June','July','August','September','October','November','December');
	$arabic_months = array('يناير','فبراير','مارس','أبريل','مايو','يونيو','يوليو','أغسطس','سبتمبر','أكتوبر','نوفمبر','ديسمبر');

	return str_replace( $arabic_months, $english_months, $string );
}

 

/**
 * Converts string containing English months into string with Arabic months
 * e.g. 1 January 2018 
 * => 1 يناير 2018
 *
 * @param string $string containing an English date
 * @return string 
 */
 
function convert_arabic_months_to_english( $string ) {
	$english_months = array('January','February','March','April','May','June','July','August','September','October','November','December');
	$arabic_months = array('يناير','فبراير','مارس','أبريل','مايو','يونيو','يوليو','أغسطس','سبتمبر','أكتوبر','نوفمبر','ديسمبر');

	return str_replace( $english_months, $arabic_months, $string );
}

 

Leave a Reply