CakePHP CakeTime utility niceshort localization in %jS - php

I use CakeTime::niceshort() and it works very well. I set correct locale values and set translate (po) files and I can see localized month, day names successfully.
But one thing is missing. When I use niceshort():
echo $this->Time->niceShort(1387120620);
I get this:
Ara 15th 2013, 15:17
But this isn't valid for Turkish dates. I need this:
Ara 15 2013, 15:17
It is possible with these:
$this->Time->format($time,"%b %e %Y, %H:%M")
But I don't want to lose niceShort's features. It gives short information if timestamp is close to now:
https://github.com/cakephp/cakephp/blob/2.5/lib/Cake/Utility/CakeTime.php#L391
Is it possible to remove "th" and "nd" from dates without changing core lib ?

You should use setlocale before calling CakeTime::niceshort()
setlocale("LC_ALL", "tr_TR.UTF-8");
You need to make sure they are properly installed on the server.

Related

date format - Regional settings - Months in lowercase

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

Problem with LC_TIME translations on CakePhp

I have a cakephp 2.9 multilanguage app and using .po files for translating strings like __('example'). So far so good but I've been trying for a while now to translate this date from:
Fri, Aug 31st 2018, 16:03
to:
Vie, Ago 31 2018, 16:03
This is my code:
setlocale("LC_ALL", "es_ES.UTF-8");
App::uses('CakeTime', 'Utility');
echo CakeTime::nice();
I've downloaded the spanish files from the localized repo (https://github.com/cakephp/localized) and put them into app\Locale\spa\LC_TIME and still the date keeps showing in english.
Can someone help me? Maybe it's the wrong approach?
Well, didn't took me long after I posted the question to realize all the ways I have tried were ok, using caketime or strftime directly, the problem was with the setlocale:
Changed this:
setlocale("LC_ALL", "es_ES.UTF-8");
to this:
setlocale(LC_TIME, 'es_ES.utf8','esp');
So my two results are:
echo ucfirst(strftime('%A '.$number_of_the_day.' %B %Y'));
//Viernes 31 agosto 2018
echo __('Today is ').CakeTime::nice();
//Hoy es vie, ago 31st 2018, 16:29
It worked like a charm, however I'd very much like a table where all this language codes are explained so we can use the right syntax for each and not guess, just like I did.

String date to time in PHP - strtotime "loses hours"

I have the behavior which is cofusing me:
1. I got rom request some string date "02.07.2014".
2. Trying to convert it to timestamp
strtotime($search->date_from)
it returns me 1404244800. Assumig that my bug is here, I'm trying to check what I get on this url http://www.onlineconversion.com/unix_time.htm and found the Tue, 01 Jul 2014 20:00:00 GMT
It's almost what I need , excluding that 4 hours are lost: it's very bad for me and unexpected.
On top of php script, put
date_default_timezone_set("YourTimeZone");
YourTimeZone can be one of this:
Timezone
The strtotime function relies on the system timezone, unless you set it otherwise.
You can set the timezone using date_default_timezone_set, example:
date_default_timezone_set("America/Phoenix");
strtotime(...);
Also, see the timezone reference
Use this link instead and it will determine your LOCAL time. http://www.epochconverter.com
The 4 hours that you are losing is most-likely due to the fact that you do not have your timezone set. You can either do this in the php.ini file or in your .php file.
In your .php file do this:
date_default_timezone_set("America/New_York"); // if you live in USA on east coast
// For a list of possible arguments, go here: http://php.net/manual/en/timezones.php
Hope this helps.

How to format dates based on locale?

In a template I display the day and month of a specific date :
<div class="jour"><?php echo date('d',strtotime($content->getCreatedAt())) ?></div>
<div class="mois"><?php echo date('M',strtotime($content->getCreatedAt())) ?></div>
This works fine, problem is the month name is in English. Where do I specify that I want the month names in another locale, French for instance ?
Symfony has a format_date helper among the Date helpers that is i18n-aware. The formats are unfortunately badly documented, see this link for a hint on them.
default_culture only applies for the symfony internationalisation framework, not for native PHP functions. If you want to change this setting project wide, I would do so in config/ProjectConfiguration.class.php, using setlocale, and then use strftime rather than date:
// config/ProjectConfigration.class.php
setlocale(LC_TIME, 'fr_FR');
// *Success.php
<div class="jour"><?php echo strftime('%d',strtotime($content->getCreatedAt())) ?></div>
<div class="mois"><?php echo strftime('%b',strtotime($content->getCreatedAt())) ?></div>
Note that this requires locale settings to be enabled on your machine. To check, do var_dump(setlocale(LC_ALL, 'fr_FR')); If the result is false, you cannot use setlocale to do this and probably need to write the translation code yourself. Furthermore, you will need to have the correct locale installed on your system. To check what locales are installed, do locale -a at the command line.
Sorry for butting in so late in the day, but I would like to add my own thoughts here. The best international date format that I have come up with is "%e %b %Y", e.g. 9 Mar 2012. I find this much more readable than the ISO format "%Y-%m-%d", e.g. 2012-03-09. According to the docs, the %x format should be locale sensitive, but it does not work for me, at least not on the iPhone. This may be because Safari is not passing the locale in the HTML headers, I do not know.
It is sometimes useful to use an array with different possible values to setlocale().
Especially to support different environments (windows, linux, ...)
setlocale(LC_TIME, array('fr', 'fr_FR', 'fr_FR.utf8', 'french', 'french_FRANCE', 'french_FRANCE.utf8'));
echo strftime("%A %d %B", strtotime(date("Y-m-d")));
As the documentation states:
If locale is an array or followed by additional parameters then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.

Zend_Date returning strange format online - works fine on my localhost

I have the following code to format dates:
$date = new Zend_Date();
$date->set(strtotime($some_date);
echo ($date->get(Zend_Date::DATE_FULL));
it wokrs fine on my localhost outputting the date as I need it to be outputted. However on my online server the dates are outputted in a weird numerical way.
i.e the numeric value fo the Day and month are shown instead of the text - it reads as 2, 2010 7 12 instead of Monday 12th July 2010
How do I fix this!
Sounds to me like it's using a different locale on the server. Try setting a locale and see what happens then: http://framework.zend.com/manual/en/zend.locale.introduction.html

Categories