close ad
 
Important WebAssist Announcement
open ad
View Menu

Web development tutorial

Display date and time in your own language format

Tutorial created by Rune Brynestad, Totalweb AS

Categories: PHP

rating

How to display date and time in your own language format using setlocale() and strftime().

arrow downDefinition and Usage

The setlocale() function sets locale information.
Locale information is language, monetary, time and other information specific for a geographical area.
Note: The setlocale() function changes the locale only for the current script.
Find your Language and Locale code: http://red-route.org/code/php-international-language-and-locale-codes-demonstration

The strftime() function formats a local time and/or date according to locale settings.
See http://www.php.net/manual/en/function.strftime.php for more information

arrow downSo how can you add your own language ?

You need to use setlocale() and strftime(). If you use the setlocale function properly, the strftime function will give you the result in your own language.

<?php setlocale (LC_TIME, "no_NO"); ?>
<?php echo strftime("%A"); ?>


This will return "Fredag" (Norwegian for the day Friday)

strftime has an optional second parameter, a timestamp that specifies the date / time you want to format.
The example above have no second parameter. Then the default date / time is now.

If you format the date field in your SQL query as timestamp, then you can add your date field to the second parameter in the strftime function.

<?php setlocale(LC_TIME, "no_NO"); ?>
<?php echo strftime('%e', strtotime($row_Recordset['Yourdate'])); ?>. <?php echo strftime('%B', strtotime($row_Recordset['Yourdate'])); ?> <?php echo date("Y", strtotime($row_Recordset['Yourdate'])); ?>


This will return
4. mars 2013 (norwegian) instead of 04. March 2013 (english default)

You can also use the same approach if you want to change time from 5 to 17:00 (12 to 24 hours)

arrow downReviews and comments

Comments will be sent to the author of this tutorial and may not be answered immediately. For general help from WebAssist, please visit technical support.

Sign in to add comments
rating

info4753: 10 Years, 10 Months, 1 Week, 3 Days, 4 Hours, 47 Minutes ago

I'd like to see a language package with DataBridge. This would save an enormous amount of time.

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.