yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

DateValidator -> timestamp generation #10737

Closed lorado closed 8 years ago

lorado commented 8 years ago

I had a DatePicker without time in UI. I had to save timestamp of the beginning of the selected day. So I supposed to get that timestamp in dependence of my timezone. But DateValidator generates the timestamp of the days beginning in UTC. So I had to make a workaround: I added a FilterValidator before, that adds to the selected date " 00:00:00", and define another format for DateValidator with time-params.

I offer to change DateValidator in line 308 from

$date = DateTime::createFromFormat($format, $value, new \DateTimeZone($hasTimeInfo ? $this->timeZone : 'UTC'));

to

$date = DateTime::createFromFormat($format, $value, new \DateTimeZone($this->timeZone));

What do you mean?

dynasource commented 8 years ago

you're suggestion removes logic. I guess its put in there by @cebe https://github.com/yiisoft/yii2/commit/d60445c066003cab78fb16458e7234fae5db4765 for a reason

lorado commented 8 years ago

Well, if I let user choose any date (without time), I suggest to get timestamp for beginning of this day in the timezone, where my app is running. Why does it remove logic? Actually this is possible only if I explicit set any time parameter (one of 'HhGgis' from php date-format) in DateValidator::format and in the value of attribute, for which I use DateValidator.

Edit: To take default UTC timezone make sense only if timezone not defined at all, not in config, not in DateValidator. But if I set the timezone for my app, I suggest result described above.

cebe commented 8 years ago

Well, if I let user choose any date (without time), I suggest to get timestamp for beginning of this day in the timezone

A pure date is an inaccurate time information, it describes a time range from 00:00 to 23:59 on that day. This information has different interpretation depended on the time zone so we avoid confusion by not trying to convert dates to timestamps as the result will be wrong in 99% of the cases.

I added a FilterValidator before, that adds to the selected date " 00:00:00", and define another format for DateValidator with time-params.

if you add the time information, then $hasTimeInfo should be true and it should add the time in your timezone. There must be something you are doing wrong here.

lorado commented 8 years ago

A pure date is an inaccurate time information, it describes a time range from 00:00 to 23:59 on that day.

Ah... that's the point... Now I got it. Thank you!