PHP date format variable

user2141311

I would like to change the date format in the following code:

#search for all event start dates
$starts = $sxml->xpath('//event/startdate');

#get the unique start dates of these event
$dates = array_unique($starts);

foreach($dates as $date) {     
   echo "<li class='header'><h1>{$date}</h1></li>" ."\n";

currently, it pulls from an XML feed as 25/11/2021 and I would like to show it as Thursday 25 November 2021 - probably as multiple variables. However, date() is not working.

Any help is appreciated!

RiggsFolly

Using the DateTime object it can be easy and accurate

$dates = ['25/11/2021','24/11/2021','23/11/2021'];

foreach ( $dates as $date){
    $df = (DateTime::CreateFromFormat('d/m/Y', $date))->format('l d F Y');
    echo "<li class='header'><h1>{$df}</h1></li>" ."\n";
}

RESULTS

<li class='header'><h1>Thursday 25 November 2021</h1></li>
<li class='header'><h1>Wednesday 24 November 2021</h1></li>
<li class='header'><h1>Tuesday 23 November 2021</h1></li>

PHP Manual for DateTime object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related