WPSEO_Sitemap_Timezone::get_datetime_with_timezone PHP Method

get_datetime_with_timezone() public method

Get the datetime object, in site's time zone, if the datetime string was valid
public get_datetime_with_timezone ( string $datetime_string ) : DateTime | null
$datetime_string string The datetime string in UTC time zone, that needs to be converted to a DateTime object.
return DateTime | null in site's time zone
    public function get_datetime_with_timezone($datetime_string)
    {
        static $utc_timezone, $local_timezone;
        if (!isset($utc_timezone)) {
            $utc_timezone = new DateTimeZone('UTC');
            $local_timezone = new DateTimeZone($this->get_timezone_string());
        }
        if (!empty($datetime_string) && WPSEO_Utils::is_valid_datetime($datetime_string)) {
            $datetime = new DateTime($datetime_string, $utc_timezone);
            $datetime->setTimezone($local_timezone);
            return $datetime;
        }
        return null;
    }

Usage Example

Beispiel #1
0
 /**
  * Get the modification date for the last modified post in the post type:
  *
  * @param array $post_types Post types to get the last modification date for.
  *
  * @return string
  */
 function get_last_modified($post_types)
 {
     global $wpdb;
     if (!is_array($post_types)) {
         $post_types = array($post_types);
     }
     // We need to do this only once, as otherwise we'd be doing a query for each post type.
     if (!is_array($this->post_type_dates)) {
         $this->post_type_dates = array();
         $query = "SELECT post_type, MAX(post_modified_gmt) AS date FROM {$wpdb->posts} WHERE post_status IN ('publish','inherit') AND post_type IN ('" . implode("','", get_post_types(array('public' => true))) . "') GROUP BY post_type ORDER BY post_modified_gmt DESC";
         $results = $wpdb->get_results($query);
         foreach ($results as $obj) {
             $this->post_type_dates[$obj->post_type] = $obj->date;
         }
         unset($query, $results, $obj);
     }
     if (count($post_types) === 1 && isset($this->post_type_dates[$post_types[0]])) {
         $result = $this->post_type_dates[$post_types[0]];
     } else {
         $result = null;
         foreach ($post_types as $post_type) {
             if (isset($this->post_type_dates[$post_type]) && strtotime($this->post_type_dates[$post_type]) > $result) {
                 $result = $this->post_type_dates[$post_type];
             }
         }
         unset($post_type);
     }
     return $this->timezone->get_datetime_with_timezone($result);
 }