I am looking for a simple way to change the language of the days of the week. I use the simple code below to call out the current day Monday, Tuesday, but I need it in French, and I've tried setting locale etc but this is not working for me.
I think arrays would do the job, but again, I could not get it to work with my limited knowledge.
<?php echo date('l'); ?>
You should use strftime() function for this - http://php.net/manual/ru/function.strftime.php. date() is not locale dependent.
i hope this will help
<?php
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %A and");
for more info click here
Related
I'm a noob (I confess it) and can't manage to find a solution to my problem.
I'm setting up the date format on Drupal and it uses the PHP date format.
Right now it's "d F Y", so it appears as 07 Dicembre 2021 (in Italian), but in Italian months are written out in lowercase. Is there a way to transform Dicembre into dicembre? I couldn't find a proper way.
Thanks for your help!
Using strtolower() the output will be as you expected:
<?php echo strtolower(date("M"),2);?>
So to implement it only for Month, simply break the date format into 3 parts. Day , Month, Year.
Use drupal module:
https://www.drupal.org/project/simple_field_formatter
Go to:
/admin/structure/types/manage/[YOURTYPE]/display
For your date field, click the gearwheel on the right (format settings) and activate the checkbox for strtolower
Or create your own FieldFormatter: https://www.webwash.net/how-to-create-a-custom-field-formatter-in-drupal-8/
You should use the locale aware strftime, which formats according to the current locale.
setlocale(LC_TIME, "it_IT");
echo strftime("%d %B %Y"); // 07 dicembre 2021
strtotime() in PHP is quite powerfull function. One of it's features is relative dates.
For example this command:
echo date('Y-m-d', strtotime('Sunday this week'));
produces 2016-02-14 on my machine (today is "2016-02-12", Friday). Thus it supposes that first day of week is Monday. However in different locales countries first day of week is different.
Is there a way to change this behaviour and make strtotime() think that first week day is Sunday?
As discussed in the comments of the question, it may be better to rely on a custom function, which is tested and will most probably produce the same result on every machine.
A function like this could be:
<?php
function x() {
return date('Y-m-d', date('N')==7 ? strtotime('today') : strtotime('last sunday'));
}
echo x();
You find a demo here.
If you have many machines to deploy your code to, you could additionally include a test script in the installation process which tests if it gets correct results from this (and other things that may vary depending on installation).
PHP 5.5 introduced the Internationalization extension, which among many useful functions provides and IntCalendar class. It has a static function getFirstDayOfWeek which can be used to get the first day of the week, based a locale.
Straight from the docs:
ini_set('date.timezone', 'UTC');
$cal1 = IntlCalendar::createInstance(NULL, 'es_ES');
var_dump($cal1->getFirstDayOfWeek()); // Monday
Got a bit of an odd question. After creating a PHP script for a friend who lives in Germany, he has decided he needs the time/date in German format, an example being:
Montag, 16.März 2014
Is there a date/time function that can do this in PHP?
Any help much appreciated, Thanks!
Use strftime() and set the locale to German
Example
<?php
setlocale(LC_ALL, "de_DE.utf8");
echo strftime("%A, %e.%B %Y");
?>
Which would output: Donnerstag, 7.August 2014
in the php.ini I defined the timezone to Europe/Athens. Everything was just fine until last sunday, when the time chanegd to WINTER TIME. The time went back in 1 hour.
The problem is, that in my website - it's still like summer time, didn't go back in 1 hour... I checked it in other website and it's ok there - http://www.timeanddate.com/worldclock/city.html?n=26
To make sure, I added this line in the top of the page:
ini_set('date.timezone', 'Europe/Athens');
But it dind't help...
What heppent? How can I fix it?
I think this will help you.
date_default_timezone_set('Europe/Athens');
If you just set the default time in the start of your function or program the date is format in the country you want.
<?php
date_default_timezone_set('Europe/Athens') ;
echo "the date is:". date("d/m/Y")."<br/>";
echo "the time is:". date("h:i:s");
?>
Just two little hints:
Please check if the output of following code returns your timezone:
$date = new DateTime();
$tz = $date->getTimezone();
echo $tz->getName();
Which function or language do you use to display the time on your website? Perhaps javascript? Then the time comes from the client system.
You might consider updating your PHP time zone database. You can find the latest version here.
However, I checked through the database and it looks like Europe/Athens has been in sync with EU since 1981, which has used the Last Sunday in October since 1996. So even if you have a very old database I can't imagine that it would be incorrect for recent dates.
I'm pulling some dates from a DB and using PHP strftime to format them.
Now, everything works as intended, apart that if I use the %A format, which is supposed to give me the full weekday name the function just returns NULL, unless the date happens to be on a weekend, in which case it correctly returns "Saturday" or "Sunday".
All the other formats work, even %a (short weekday name).
It does not seem to depend on the locale I use, nor on the specific format of the date (same problem happens if I just use strftime on mktime.
My only thought is that it's some sort of incredibly weird configuration problem server side, but I'd like to hear if anyone had other ideas about it...
EDIT: some code, although it's pretty much what I have written before...
$id = (int)$_GET['event'];
$res = mysql_query("SELECT date FROM events WHERE event_id=".$id);
$row = mysql_fetch_array($res);
echo strftime("%a %H:%M", $row['date']);
echo strftime("%A %H:%M", $row['date']);
The first echo works fine, returning for instance Thu 15:30, the second returns NULL unless $row['date'] falls on a Saturday or Sunday.
If this may be of any help the code is inside a class, but I can't see how this may affect the result...
EDIT2: This is NOT a problem with the DB or the date format, otherwise the first echo wouldn't work. Also, I can just remove the DB code, and generate the date with mktime or with strtotime and it still doesn't work.
EDIT3 (solution): Found the issue. In Italian, the names of the days end in ì (e.g. lunedì), apart from Saturday and Sunday, which do not have accented letters. The result of the operation was passed to json_encode which apparently doesn't like accented characters... A call to utf8_encode (or to htmlentities) solved the issue.
According to the manual : http://php.net/manual/en/function.strftime.php
If you're passing something other than a timestamp you're doing it wrong. Can't really say why the first one passes and the second one doesn't. Maybe PHP is trying to compensate. In any case, if you have a text time representation, you need to call strtotime() on it first.
EDIT
I ran the following code in my system
$row['date'] = '2011-04-06 08:33:29';
echo strftime("%a %H:%M", $row['date']);
echo '<br>';
echo strftime("%A %H:%M", $row['date']);
And I got this as the output
Notice: A non well formed numeric value encountered in F:\webroot\utils\test.php on line 4
Thu 00:33
Notice: A non well formed numeric value encountered in F:\webroot\utils\test.php on line 6
Thursday 00:33
You should have notices enabled on your system. Changing it to timestamp should solve it.
EDIT 2
...Also, I can just remove the DB code,
and generate the date with mktime or
with strtotime and it still doesn't
work
If you could post the sample that doesn't work we could have a look
In your comments you say that your database contains dates such as 2011-04-06 08:33:29. But the second argument to strftime should be a unix timestamp such as 1302766547, that is the number of seconds since 1970-01-01 00:00:00 GMT. Try this instead:
echo strftime('%a %H:%M', strtotime($row['date']));