Email

nazmul.hossain@nihal.one

Home / Blog / Display Date in Any Language Using PHP

Display Date in Any Language Using PHP

by | Feb 20, 2012 | 0 comments

If your site is in a different language than English then you must need to display date and time in that language in your site.It is very easy to display date in any language with php.

You can do this by using str_replace() function.All you have to do replace the numbers, months and day names with your wanted language’s words.
Str_replace() function is used to replace parts of a PHP string with new value.

I am going to create a function named translated_date_and_time()  and do every thing inside that and when I call this function anywhere in my php script it will display date in the language that I want.I am going to use Bengali here.

Definition of the translator function:

	function translated_date_and_time()
	{
		//First take the value of date() function
		$date = date('g:i A F j, Y');
		 
		//Translate the numbers
		 
		$english_numbers = array(0,1,2,3,4,5,6,7,8,9);
		$translated_numbers = array('০','১','২','৩','৪','৫','৬','৭','৮','৯');
		 
		$date = str_replace($english_numbers,$translated_numbers,$date);
		 
		//Translate the months
		 
		$english_months = array(
			'January',
			'February',
			'March',
			'April',
			'May',
			'June',
			'July',
			'August',
			'September',
			'October',
			'November',
			'December'
		);
		
		$translated_months = array(
			'জানুয়ারি',
			'ফেব্রুআরি',
			'মার্চ',
			'এপ্রিল',
			'মে',
			'জুন',
			'জুলাই',
			'আগস্ট',
			'সেপ্টেমর',
			'অক্টবর',
			'নভেম্বর',
			'ডিসেম্বর'
		);
		 
		$date = str_replace($english_months,$translated_months,$date);
		 
		//Translate Am and PM
		 
		$english_am_pm = array("AM","PM");
		$translated_am_pm = array('এম','পিএম');
		 
		$date = str_replace($english_am_pm,$translated_am_pm,$date);
		 
		return $date;
	}

Now it is time to call our function:

echo translated_date_and_time();

. . .

This was originally posted on one of my old blog – CodingWar.com

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Goodreads Updates

Goodreads Read Shelf

Steve Jobs
Zero to One: Notes on Startups, or How to Build the Future
The Tipping Point: How Little Things Can Make a Big Difference
The Fault in Our Stars
Harry Potter and the Sorcerer's Stone
The One Thing: The Surprisingly Simple Truth Behind Extraordinary Results
How to Win Friends and Influence People
Outliers: The Story of Success
Rich Dad, Poor Dad
The Power of Habit: Why We Do What We Do in Life and Business
Who Moved My Cheese?
Blink: The Power of Thinking Without Thinking
Good to Great: Why Some Companies Make the Leap... and Others Don't
Sapiens: A Brief History of Humankind
We Should All Be Feminists
A Brief History of Time
The Happiness Project: Or Why I Spent a Year Trying to Sing in the Morning, Clean My Closets, Fight Right, Read Aristotle, and Generally Have More Fun
What the Dog Saw and Other Adventures
Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones
David and Goliath: Underdogs, Misfits, and the Art of Battling Giants