setlocale doesn't work properly in my timezone - php

I live in Bangladesh. But when I use this code.
<?php setlocale(LC_ALL,"asia/dhaka");
echo strftime("%d %B %Y"); ?>
Result shows US Timezone. I want to change my timezone.

Locale and time zone are not the same concept. For example, one can have a US English locale and have their clock set to Japan's local time (perhaps they are traveling).
For PHP, use the date_default_timezone_set function to set the time zone - not the setlocale function.

Related

PHP strftime outputs wrong format despite correct timezone

I configured my timezone to Europe/Paris in php.ini. When executing date_default_timezone_get() I do get the correct value.
Then, I expect strftime('%x', date()) to output something like 16 novembre 2018 which is the French format. But instead, I get 11/16/2018 which looks like the US format.
Any idea why?
The time zone has no effect on how dates and times are presented, for that you need to set the locale. There are no standards for locale names, but fortunately PHP's setlocale() function will take multiple locale names, stopping at the first successful one.
// just a few common name formats
setlocale(LC_TIME, ["fr_FR.utf8", "fr_FR#euro", "fr_UTF8", "fr_FR", "french"]);
echo strftime("%d %B %Y", time());
I tried:
setlocale(LC_ALL, 'fr_utf8');
echo strftime("%d %B %Y", time());
and got:
16 novembre 2018
after setting the timezone to Europe/Paris, Use echo date('d-M-Y') and it will display your desired time. it worked for me

how to set date and time to dutch

I am using this simple script to test on my host which runs on php version 5.6.
The script gives the output "Wednesday" instead off "Woensdag"(dutch)
Why is the day of the week still in English and not in Dutch?
Is this because the server is not correct configured?
<?php
/* Set locale to Dutch */
date_default_timezone_set("Europe/Amsterdam");
setlocale(LC_ALL, 'nl_NL');
echo date("l"); // output: Wednesday instead off "Woensdag" (dutch)
?>
Unfortunately date is not multilingual. If you want to format a language in another language you need to set locale (as you did in your example) and use strftime
Formatting options for strftime do result in "Woensdag" (or "mittwoch in German, etc):
setlocale(LC_TIME, 'en_EN');
echo strftime('%A', time()); // for a Wednesday will output: Wednesday
setlocale(LC_TIME, 'nl_NL');
echo strftime('%A', time()); // for a Wednesday will output: woensdag
Unless you want al lot of things to be influenced by setlocale you might want to specify that you're doing this for time only (as in the example above). Read on setlocale for more info: http://php.net/manual/en/function.setlocale.php
Keep in mind you need to have the locales available on your machine for this to work. Check with locale -a on linux. setlocale has a return value. If it is false something went wrong and you're most likely missing the specified locale.

PHP gmdate() returning UTC time an hour behind

So I have a problem whereby when I use the php gmdate() function on my local machine web server it returns the correct UTC time but when I upload the same script to a vps server the function returns a UTC time that is about and hour behind.I am using the UTC time together with javascript to display local time to different clients.
This is how i have called the function:
gmdate('m/d/Y H:i:s', time());
Any help would be appreciated.
It could be the server's timezone or even the default PHP timezone. You can override this using the following function date_default_timezone_set()
date_default_timezone_set('America/New_York');
http://php.net/manual/en/function.date-default-timezone-set.php
You can find a list of supported timezone identifiers here: http://www.php.net/manual/en/timezones.php
Set your date_default_timezone_set() using PHP
Eg:
<?php
date_default_timezone_set("Asia/Bangkok"); // use your local timezone here
echo date_default_timezone_get();
?>
for more click here: http://php.net/manual/en/function.date-default-timezone-set.php

Why is DATE_FORMAT() in php a few hours behind?

The form I am using to submit data to the database is processing the time incorrectly (usually a few hours behind the actual time). The output below was really done at 9:28 not 7:28. How do I fix this so that it processes the time correctly? I do not want to use military time. Is it possible that it could be something with my website hosting service? I tried this on XAMPP and everything works fine. Any help would be greatly appreciated.
Code:
DATE_FORMAT(posts.post_date, '%W, %M %e, %Y at %h:%i %p') AS date
Output:
Saturday, July 6, 2013 at 07:28 PM
The time would go by your server time. An easy workaround for this is to manually set the timezone before the date() or time() functions are called to.
I'm in Kolkata, India so I have something like this:
date_default_timezone_set('Asia/Kolkata');
Or another example is LA - US:
date_default_timezone_set('America/Los_Angeles');
You can also see what timezone the server is currently in via:
date_default_timezone_get();
So something like:
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
So the short answer for your question would be:
// Change the line below to your timezone!
date_default_timezone_set('Asia/Kolkata');
$date = date('m/d/Y h:i:s a', time());
Then all the times would be to the timezone you just set :)
As I commented above, you can set the default timezone for your dates and times using
date_default_timezone_set(). The documentation can be found here.
The list of supported timezones can be found here. You can pass your default timezone as a parameter in the aforementioned method.
If you turn E_NOTICE on, you'd probably work this one out as it chucks up notices if you use date/time functions without setting the time zone.
bool date_default_timezone_set ( string $timezone_identifier )
E.G:
<?php
date_default_timezone_set('America/Los_Angeles');
http://www.php.net/manual/en/function.date-default-timezone-set.php

Change date helper language Codeigniter

I'm using Codeigniter for display dates.
I have this but it's echoed in english, how do I set it to other language? I already have the spanish language pack, but I can't figure it out how to load it.
$this->load->helper('date');
echo mdate("%F %d, %Y", strtotime(now()));
Thanks
// Set locale to Spanish Argentina, change to your installed language
setlocale(LC_TIME, 'es_AR');
echo strftime("%B %d, %Y", time());
First you do not need to use the date helper to accomplish this. mdate() is essentially the same as strftime() where just the vars are slightly different. Also your code strtotime(now()) is wrong. now() is the exact same as time() as indicated in the CI documentation. It returns a UNIX timestamp, strtotime() converts a string to a UNIX timestamp. So what you were doing was trying to convert a timestamp to a timestamp, which of course is wrong. I changed the code so it doesn't require the helper and correctly will output MONTHNAME DATE, YEAR in the specified language. Make sure you change the es_AR to whatever Spanish language you installed. It will be the in the format es_COUNTRYCODE
The traductions dates in "calendar_lang.php"
$this->config->set_item('language', 'spanish');
$this->load->library('calendar');
echo 'Mes:'.$this->calendar->get_month_name(date('m'));
echo 'Día:'.$this->calendar->get_day_names(date('d'));

Categories