Convert Mysql date to Google sitemap date format PHP

Convert your MySql format date to the date that can be readable to Google sitemaps API using PHP

    $updated_date = '2021-01-28 11:52:19';

    $new_datetime = new DateTime($updated_date);
    $final_date = $new_datetime->format('Y-m-d\TH:i:sP');

    echo $final_date; //2021-01-28T11:52:19+01:00

In the code snippet, we have stored the date to our MySql database and when we try to create a sitemap, we need to change the format of the date so that the google sitemap API could red it unless it will throw the error. We are using the DateTime function of PHP for that and then its 'format' method to get the desired format date.

Was this helpful?