John Davidson

php - Implode array of dates and change date format

0 comments
Message:


I have the following code of which I want to retrieve dates from db. The code outputs the disered list, but in different date format.


$array = $stmt->fetchAll(PDO::FETCH_COLUMN);
$dates = implode("\n",$array);
return $dates;

Result is:


2021-05-26
2021-05-27
2021-05-28
2021-05-29
2021-05-30

I need this format: m/d/Y


I tried this:


`$array = $stmt->fetchAll(PDO::FETCH_COLUMN);

$dates = implode("\n",$array);
$newDate = date("m/d/y", strtotime($dates));
return $newDate;`


But the result is just one result and wrong:


01/01/70


Format changed but i lost the correct dates


Thanks in advance.



My Answer:
fetchAll(PDO::FETCH_COLUMN);

$dates = implode(", ", $array);

foreach($array as $date){
$newDate = date("Y-m-d", strtotime($date));
echo $newDate . "
";
}

Rate this post

3 of 5 based on 5681 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us