| 1007 | | * Retrieve the comment time of the current comment. |
| | 1009 | * Retrieve comment published time as a `DateTimeImmutable` object instance. |
| | 1010 | * |
| | 1011 | * The object will be set to the timezone from WordPress settings. |
| | 1012 | * |
| | 1013 | * For legacy reasons, this function allows to choose to instantiate from local or UTC time in database. |
| | 1014 | * Normally this should make no difference to the result. However, the values might get out of sync in database, |
| | 1015 | * typically because of timezone setting changes. The parameter ensures the ability to reproduce backwards |
| | 1016 | * compatible behaviors in such cases. |
| | 1017 | * |
| | 1018 | * @since 5.4.0 |
| | 1019 | * |
| | 1020 | * @param int|WP_Comment $comment Optional. WP_Comment object or ID. Default is global `$comment` object. |
| | 1021 | * @param string $source Optional. Local or UTC time to use from database. Accepts 'local' or 'gmt'. |
| | 1022 | * Default 'local'. |
| | 1023 | * @return DateTimeImmutable|false Time object on success, false on failure. |
| | 1024 | */ |
| | 1025 | function get_comment_datetime( $comment = null, $source = 'local' ) { |
| | 1026 | $comment = get_comment( $comment ); |
| | 1027 | |
| | 1028 | if ( ! $comment ) { |
| | 1029 | return false; |
| | 1030 | } |
| | 1031 | |
| | 1032 | $wp_timezone = wp_timezone(); |
| | 1033 | |
| | 1034 | if ( 'gmt' === $source ) { |
| | 1035 | $time = $comment->comment_date_gmt; |
| | 1036 | $timezone = new DateTimeZone( 'UTC' ); |
| | 1037 | } else { |
| | 1038 | $time = $comment->comment_date; |
| | 1039 | $timezone = $wp_timezone; |
| | 1040 | } |
| | 1041 | |
| | 1042 | if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) { |
| | 1043 | return false; |
| | 1044 | } |
| | 1045 | |
| | 1046 | $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timezone ); |
| | 1047 | |
| | 1048 | if ( false === $datetime ) { |
| | 1049 | return false; |
| | 1050 | } |
| | 1051 | |
| | 1052 | return $datetime->setTimezone( $wp_timezone ); |
| | 1053 | } |
| | 1054 | |
| | 1055 | /** |
| | 1056 | * Retrieve comment published or modified time as a Unix timestamp. |
| | 1057 | * |
| | 1058 | * Note that this function returns a true Unix timestamp, not summed with timezone offset |
| | 1059 | * like older WP functions. |
| | 1060 | * |
| | 1061 | * @since 5.4.0 |
| | 1062 | * |
| | 1063 | * @param int|WP_Comment $comment Optional. WP_Comment object or ID. Default is global `$comment` object. |
| | 1064 | * @return int|false Unix timestamp on success, false on failure. |
| | 1065 | */ |
| | 1066 | function get_comment_timestamp( $comment = null ) { |
| | 1067 | $datetime = get_comment_datetime( $comment, $field ); |
| | 1068 | |
| | 1069 | if ( false === $datetime ) { |
| | 1070 | return false; |
| | 1071 | } |
| | 1072 | |
| | 1073 | return $datetime->getTimestamp(); |
| | 1074 | } |
| | 1075 | |
| | 1076 | |
| | 1077 | /** |
| | 1078 | |
| | 1079 | |
| | 1080 | /** |
| | 1081 | * Retrieve the comment time for a comment. |
| 1020 | | $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date; |
| 1021 | | if ( '' == $d ) { |
| 1022 | | $date = mysql2date( get_option( 'time_format' ), $comment_date, $translate ); |
| | 1095 | $source = ( $gmt ) ? 'gmt' : 'local'; |
| | 1096 | $datetime = get_comment_datetime( $comment, $source ); |
| | 1097 | |
| | 1098 | if ( false === $datetime ) { |
| | 1099 | return false; |
| | 1100 | } |
| | 1101 | |
| | 1102 | if ( '' === $d ) { |
| | 1103 | $d = get_option( 'time_format' ); |
| | 1104 | } |
| | 1105 | |
| | 1106 | if ( 'U' === $d || 'G' === $d ) { |
| | 1107 | $time = $datetime->getTimestamp(); |
| | 1108 | |
| | 1109 | // Returns a sum of timestamp with timezone offset. Ideally should never be used. |
| | 1110 | if ( ! $gmt ) { |
| | 1111 | $time += $datetime->getOffset(); |
| | 1112 | } |
| | 1113 | } elseif ( $translate ) { |
| | 1114 | $time = wp_date( $d, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null ); |