I need to echo internationalized time in different format for different languages.
Now i am using locale when creating a date, but it also outputs time:
$date->toString();
29.05.2012 0:00:00
Expected result:
29.05.2012
In documentation i can see inly custom formats (as i understand no internationalization format will be applied if i use them). Is it possible to output only date, without time, like described there (as i understand zf supports international formats: http://en.wikipedia.org/wiki/Date_format_by_country)?
I tried:
echo $date->get(Zend_Date::DATE_SHORT, $locale);
echo $date->toString(Zend_Date::DATE_SHORT); ?>
// produces MMMMMMMM (letters instead of date? Oo)
It didn't work because it was necessary to add
Zend_Date::setOptions(array('format_type' => 'iso'));
in bootstrap instead of
Zend_Date::setOptions(array('format_type' => 'php'));
With this option, it works fine!
Related
I would like to check the given date and time against the following formats.
Date -- {YYYYMMDD},
Time need to check with four formats -- {HHMM, or HHMMSS, or HHMMSSD, or HHMMSSDD}
Above date and time formats needs to be checked using freemarker,
How to check and validate in freemarker? ..
Please suggest
Validation (like input validation) is normally not done in templates. So if you parse strings with ?date(pattern)/?time(pattern), and the format doesn't match, the whole template terminates with error. If you just need to figure out which of those formats a string input is in, you could use the length (?length) and/or regular expressions (?matches(regexp)).
I'm migrating a whole bunch of web pages that were written in classic asp over to a new server, and have discovered many references to the simple date() function, like:
if cint(left(date,instr(date,"/")-1)) < 9 then blah blah
I'm getting errors because the new server's default date format is returning yyyy-mm-dd, and the code above is expecting it to be in dd/mm/yyyy format.
Rather than manually fixing every occurrence, of which there could be hundreds, I'm looking to see if I can change the default date format for asp so that date() returns dd/mm/yyyy. I thought by simply changing the system's short date format would do the trick, but even after restarting the server it's still showing yyyy-mm-dd.
Is there a setting somewhere where you can specify the default date format when using the date() function?
This worked for me:
change global.asa, in the Sub Session_OnStart, add a line
Session.LCID=1033
I'm working on an app that allows the user to edit several dates in a form. The dates are rendered in the European format (DD-MM-YYYY) while the databases uses the default YYYY-MM-DD format.
There are several ways to encode/decode this data back and forth from the database to the user, but they all require a lot of code:
Use a helper function to convert the date before saving and after retrieving (very cumbersome, requires much code)
Create a separate attribute for each date attribute, and use the setNameAttribute and getNameAttribute methods to decode/encode (also cumbersome and ugly, requires extra translations/rules for each attribute)
Use JavaScript to convert the dates when loading and submitting the form (not very reliable)
So what's the most efficient way to store, retrieve and validate dates and times from the user?
At some point, you have to convert the date from the view format to the database format. As you mentioned, there are a number of places to do this, basically choosing between the back-end or the front-end.
I do the conversion at the client side (front-end) using javascript (you can use http://momentjs.com to help with this). The reason is that you may need different formats depending on the locale the client is using (set in the browser or in his profile preferences for example). Doing the format conversion in the front-end allows you to convert to these different date formats easily.
Another advantage is that you can then use the protected $dates property in your model to have Laravel handle (get and set) these dates automatically as a Carbon object, without the need for you to do this (see https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L126).
As for validation, you need can then use Laravel's built-in validation rules for dates, like this:
'date' => 'required|date|date_format:Y-n-j'
While client-side is good for UX, it doesn't let you be sure, all will be good.
At some point you will need server-side validation/convertion anyway.
But here's the thing, it's as easy as this:
// after making sure it's valid date in your format
// $dateInput = '21-02-2014'
$dateLocale = DateTime::createFromFormat('d-m-Y', $dateInput);
// or providing users timezone
$dateLocale =
DateTime::createFromFormat('d-m-Y', $dateInput, new DateTime('Europe/London'));
$dateToSave = $dateLocale
// ->setTimeZone(new TimeZone('UTC')) if necessary
->format('Y-m-d');
et voila!
Obviously, you can use brilliant Carbon to make it even easier:
$dateToSave = Carbon::createFromFormat('d-m-Y', $dateInput, 'Europe/London')
->tz('UTC')
->toDateString(); // '2014-02-21'
Validation
You say that Carbon throws exception if provided with wrong input. Of course, but here's what you need to validate the date:
'regex:/\d{1,2}-\d{1,2}-\d{4}/|date_format:d-m-Y'
// accepts 1-2-2014, 01-02-2014
// doesn't accept 01-02-14
This regex part is necessary, if you wish to make sure year part is 4digit, since PHP would consider date 01-02-14 valid, despite using Y format character (making year = 0014).
The best way I found is overriding the fromDateTime from Eloquent.
class ExtendedEloquent extends Eloquent {
public function fromDateTime($value)
{
// If the value is in simple day, month, year format, we will format it using that setup.
// To keep using Eloquent's original fromDateTime method, we'll convert the date to timestamp,
// because Eloquent already handle timestamp.
if (preg_match('/^(\d{2})\/(\d{2})\/(\d{4})$/', $value)) {
$value = Carbon\Carbon::createFromFormat('d/m/Y', $value)
->startOfDay()
->getTimestamp();
}
return parent::fromDateTime($value);
}
}
I'm new in PHP, so I don't know if it's the best approach.
Hope it helps.
Edit:
Of course, remember to set all your dates properties in dates inside your model. eg:
protected $dates = array('IssueDate', 'SomeDate');
When I type:
Yii::app()->getLocale()->dateFormat
it gives me the correct dateformat for the current set language. (in my example it is 'de' => dd.MM.yyyy). But when I type:
Yii::app()->format->dateFormat
Yii gives me the date format for 'en_us' (Y/m/d).
With getLocale() I will get only the string saved in i18n file. In ->format->date() this format string should be used, but I don't find a way to assign the i18n string to the CDateFormatter or CFormatter Object.
The CFormatter component accessed with Yii::app()->format is not meant to be used for localization out of the box; it does not automatically work according to the application locale.
You could manually change the relevant properties on Yii::app()->format to bring it in line with the application locale, but there is a more convenient way to format dates:
Yii::app()->locale->dateFormatter->formatDateTime(...)
See CDateFormatter::formatDateTime for more information; if you want more control, there are other methods such as CDateFormatter::format available. Also keep in mind that CLocale::getNumberFormatter() is available to format numbers.
I am developing a site Zend based but I have the followin problem:
$currency = new Zend_Currency();
$currency->toCurrency(20, array('currency' => 'EUR','number_format' => '#0.#'));
Gives me 20,00 EUR but I need 20.00 EUR (that is why i set number_format), Any suggestion?
Try changing number_format to just format.
Excerpt from Zend_Currency Docs in reference to the options array('format' =>'#0.00'): format: Defines the format which
should be used for displaying numbers. This number-format includes for
example the thousand separator. You can either use a default format by
giving a locale identifier, or define the number-format manually. If
no format is set the locale from the Zend_Currency object will be
used.