ZF3 get date/time format based on locale - php

I'm looking for a way to get the date and time or date/time format based on the user locale I have.
Like:
de_DE will have d-m-Y (21-10-2017)
en_US will have Y/m/d (2017/10/21)
Is there a way in ZF3 to get this or do I need to use some PHP based solution ?
It's not the idea to get the anything formatted but the format itself.

What you are probably looking for is the zend-i18n view helpers.
If unset, it will use the default locale (return value of Locale::getDefault()).
So if you use it without setting the last parameter, you should be able to use the language defined in your previous topic!

Related

Convert manually entered PHP timezone to a "valid" one

We have an application which allows users to enter their timezone manually (via a regular text box).
Since I don't think this was a good idea, I was thinking about replacing this mechanism by showing a drop-down list which lists all of the supported PHP timezones (by using DateTimeZone::listIdentifiers()).
However, I am not sure how to convert the user input (like 'GMT', or other timezones listed here: http://php.net/manual/en/timezones.others.php) to a timezone which is listed as a result of the listIdentifiers() function.
Any suggestions?
Thank you!
I'm not sure what you're trying to do. First you're writing that you have a text input and want to replace it with selectbox but finally you want to convert the value from text input? :p
If you have a value from selectbox with proper identifier you can just create a new DateTimeZone object with name as argument like this:
new DateTimeZone($str);
After that you can do anything with it.

PHP Date validation English to American format

I've come across many date based questions and solutions, but nothing that is quite what I'm used to.
I'm working on a site for the USA market, therefore the dates are commonly m/d/Y. However the English and other English speaking countries will also use it and they are likely to want to put d/m/Y.
It seems PHP Date can understand the former, but not the latter. If I could guarantee the use of d/m/Y and not either it would be simple.
Has anyone come across a solution that can detect the type of date and make the necessary correction for processing?
strftime() and other date-time functions are locale dependent. Use setlocale() to pre-define the formats you want php to use. E.g.:
setlocale(LC_TIME, 'en_UK');

Converting GMT value for DateTimeZone?

I'm currently using this to convert a timestamp to a user's defined timezone. My problem is that DateTimeZone() requires a timezone like Europe/Vienna or America/Chicago:
$date = new DateTime("#".$timestamp);
$date->setTimezone(new DateTimeZone("America/Chicago"));
I already looked into supported timezones on http://us3.php.net/manual/en/timezones.php but there are so many of them and I don't want users to browse the whole list.
Is there a simple way converting e.g. GMT+1:00, GMT-4:30 or GMT+5:45 to a correct value for DateTimeZone().
Or is it better to use an array list like I found here: Convert selected time and timezone to a set timezone
Is it better to use UTC or GMT in general for the user to pick?
Thanks!
You should probably obtain the user's real timezone (selected from the complete list). Using only a static offset from UTC you will not be able to follow the correct daylight savings time rules for the user's location.
PHP's DateTimeZone doesn't appear to accept POSIX timezone strings so it looks like you're stuck with predefined timezones.
Look in /usr/share/zoneinfo/Etc. There are a bunch of timezones predefined there for UTC plus and minus an integer. The only gotcha is that the sense of + and - is reversed from the normal convention. So just use, for example, "Etc/GMT-9" for a generic timezone with offset +0900.
This doesn't handle all of the possible timezones, like Nepal's weird +0545 but it looks like it's the best option that's easily accessible.
I don't think there's another way to pass a timezone. I think the time zones used in PHP have to be location-based to determine the DST settings along with the offset.
However, you can still present the options as GMT+1:00 etc to your users. And you can narrow down the list to only the much-used options (UTC, GMT, PST, etc).

Yii custom date format (pattern)

Is there a way to specify your own date pattern besides the included ones (small, medium, full). The main point here is that it should work with i18n. I've tried a couple of things but I couldn't get it to work...
Yii::app()->dateFormatter->format("l d/m/Y",$slide->date_start);
I know about strftime but the problem here is that different hosting providers use different locale string... and you have to customize it...
I'm looking for an elegant way of doing this.
I'd like to display the date in l d/m/Y form...
Update:
Never mind... I've just found out that dateFormatter doesn't use standard php date format...
I think you should measure time solely in Unix Time because Timezones & date formats are a presentation-layer problem. Unix time is always UTC & It's a single number, so easier to pass around in code.
As far the problem of "hosting providers use different locale string", just ask the user his timezone & display according to that. far less error-prone than trying to guess.
For date formatting, have a look at YII's format()
Hope it answers your question
Here's a related yii forum discussion
The yii forum solution worked for me to avoid raw SQL NOW() statements but still produce database-friendly date strings with PHP date() and time() functions which otherwise return integers.
In protected/config/main.php:
...
'params'=>array(
'mysqlDateTimeFormat' => 'Y-m-d H:i:s', # ':u' adds microsecond precision,
...
Then, wherever you want to put a date-time string into a model field use
$myModel->myDate = date(Yii::app()->params['mysqlDateTimeFormat']);
Obviously you can enter the date/time format into the date (or time) functions directly if you prefer.

Zend Date MySQL Timezone offset problem

I've got a site which uses the Zend Framework. There's a form which the users fill in, including a Date field. Currently I'm using this to create a new Zend_Date object and then getting the date in ISO format to put into the MySQL database. However when the date is returned in ISO format it also has the timezone offset appended to the end (e.g. 2011-01-01T00:00:00-0500), which MySQL doesn't like. When I try to add it to the DB it gives me an invalid date error. I'm sure there must be a simple solution to return the date without the timezone offset, but I can't seem to find it.
Any suggestions?
Thanks.
if you toString your Zend_Date object and then use the mb_strcut() function to remove the timezone and then insert it into the db it should be fine ?
another way would be to alter the Zend function that returns the date in an ISO format to prevent it from appending the timezone to the end of the date.

Categories