@@ -584,10 +584,10 @@ public function asDatetime($value, $format = null)
584
584
private function formatDateTimeValue ($ value , $ format , $ type )
585
585
{
586
586
$ timeZone = $ this ->timeZone ;
587
- // avoid time zone conversion for date-only values
588
- if ($ type === 'date ' ) {
589
- list ($ timestamp , $ hasTimeInfo ) = $ this ->normalizeDatetimeValue ($ value , true );
590
- if (!$ hasTimeInfo ) {
587
+ // avoid time zone conversion for date-only and time-only values
588
+ if ($ type === 'date ' || $ type === ' time ' ) {
589
+ list ($ timestamp , $ hasTimeInfo, $ hasDateInfo ) = $ this ->normalizeDatetimeValue ($ value , true );
590
+ if ($ type === ' date ' && !$ hasTimeInfo || $ type === ' time ' && ! $ hasDateInfo ) {
591
591
$ timeZone = $ this ->defaultTimeZone ;
592
592
}
593
593
} else {
@@ -650,40 +650,47 @@ private function formatDateTimeValue($value, $format, $type)
650
650
* The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given.
651
651
* - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object
652
652
*
653
- * @param bool $checkTimeInfo whether to also check if the date/time value has some time information attached.
653
+ * @param bool $checkDateTimeInfo whether to also check if the date/time value has some time and date information attached.
654
654
* Defaults to `false`. If `true`, the method will then return an array with the first element being the normalized
655
- * timestamp and the second a boolean indicating whether the timestamp has time information or it is just a date value.
655
+ * timestamp, the second a boolean indicating whether the timestamp has time information and third a boolean indicating
656
+ * whether the timestamp has date information.
656
657
* This parameter is available since version 2.0.1.
657
658
* @return DateTime|array the normalized datetime value.
658
659
* Since version 2.0.1 this may also return an array if `$checkTimeInfo` is true.
659
660
* The first element of the array is the normalized timestamp and the second is a boolean indicating whether
660
661
* the timestamp has time information or it is just a date value.
662
+ * Since version 2.0.12 the array has third boolean element indicating whether the timestamp has date information
663
+ * or it is just a time value.
661
664
* @throws InvalidParamException if the input value can not be evaluated as a date value.
662
665
*/
663
- protected function normalizeDatetimeValue ($ value , $ checkTimeInfo = false )
666
+ protected function normalizeDatetimeValue ($ value , $ checkDateTimeInfo = false )
664
667
{
665
668
// checking for DateTime and DateTimeInterface is not redundant, DateTimeInterface is only in PHP>5.5
666
669
if ($ value === null || $ value instanceof DateTime || $ value instanceof DateTimeInterface) {
667
670
// skip any processing
668
- return $ checkTimeInfo ? [$ value , true ] : $ value ;
671
+ return $ checkDateTimeInfo ? [$ value, true , true ] : $ value ;
669
672
}
670
673
if (empty ($ value )) {
671
674
$ value = 0 ;
672
675
}
673
676
try {
674
677
if (is_numeric ($ value )) { // process as unix timestamp, which is always in UTC
675
678
$ timestamp = new DateTime ('@ ' . (int )$ value , new DateTimeZone ('UTC ' ));
676
- return $ checkTimeInfo ? [$ timestamp , true ] : $ timestamp ;
679
+ return $ checkDateTimeInfo ? [$ timestamp, true , true ] : $ timestamp ;
677
680
} elseif (($ timestamp = DateTime::createFromFormat ('Y-m-d ' , $ value , new DateTimeZone ($ this ->defaultTimeZone ))) !== false ) { // try Y-m-d format (support invalid dates like 2012-13-01)
678
- return $ checkTimeInfo ? [$ timestamp , false ] : $ timestamp ;
681
+ return $ checkDateTimeInfo ? [$ timestamp , false , true ] : $ timestamp ;
679
682
} elseif (($ timestamp = DateTime::createFromFormat ('Y-m-d H:i:s ' , $ value , new DateTimeZone ($ this ->defaultTimeZone ))) !== false ) { // try Y-m-d H:i:s format (support invalid dates like 2012-13-01 12:63:12)
680
- return $ checkTimeInfo ? [$ timestamp , true ] : $ timestamp ;
683
+ return $ checkDateTimeInfo ? [$ timestamp, true , true ] : $ timestamp ;
681
684
}
682
685
// finally try to create a DateTime object with the value
683
- if ($ checkTimeInfo ) {
686
+ if ($ checkDateTimeInfo ) {
684
687
$ timestamp = new DateTime ($ value , new DateTimeZone ($ this ->defaultTimeZone ));
685
688
$ info = date_parse ($ value );
686
- return [$ timestamp , !($ info ['hour ' ] === false && $ info ['minute ' ] === false && $ info ['second ' ] === false )];
689
+ return [
690
+ $ timestamp ,
691
+ !($ info ['hour ' ] === false && $ info ['minute ' ] === false && $ info ['second ' ] === false ),
692
+ !($ info ['year ' ] === false && $ info ['month ' ] === false && $ info ['day ' ] === false )
693
+ ];
687
694
} else {
688
695
return new DateTime ($ value , new DateTimeZone ($ this ->defaultTimeZone ));
689
696
}
0 commit comments