-1

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.

Fabrizio
  • 11
  • 2
  • 1
    Which part of this are you having trouble with? Map the array with a [date formatting](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) function. – El_Vanja Jun 08 '21 at 13:06
  • I uploaded more information, sorry my first ask on stackoverflow. I m learning and i m not expert. My problem is combine the strtotime fuction with implode. Thanks a lot – Fabrizio Jun 08 '21 at 13:33

3 Answers3

1

You can use array_map to apply a callback to all elements of an array. With the strtotime and date function, you can change the format of your dates. Documentation on php.net

$array = $stmt->fetchAll(PDO::FETCH_COLUMN);
$dates = array_map(function($element) {
    return date("Ymd", $element);
}, $dates);
$dates = implode("\n",$array);
return $dates;
D B
  • 534
  • 3
  • 14
1

use the mysql function DATE_FORMAT in your SELECT and you don't need to reformat it.

SELECT DATE_FORMAT('2021-05-26','%m/%d/%Y')
| DATE_FORMAT('2021-05-26','%m/%d/%Y') |
| :----------------------------------- |
| 05/26/2021                           |

db<>fiddle here

In your query you use it like advertised and replacwe the date with the column

SELECT  DATE_FORMAT(`date`,'%m/%d/%Y') FROM reservations WHERE object_id IN (select id from rooms WHERE sub_id='$value')

your code is vulnerable to sql injection so use prepared statements with parameters see How can I prevent SQL injection in PHP?

nbk
  • 45,398
  • 8
  • 30
  • 47
  • Thank a lot for answers. My query is exactly this: "SELECT date FROM reservations WHERE object_id IN (select id from rooms WHERE sub_id='$value')" How i can use that fuction you saied? – Fabrizio Jun 08 '21 at 13:48
  • i added the query, but your code is **vulnerable** to **sql injection** so use **prepared statements with parameters** see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Jun 08 '21 at 13:57
  • Thank you very much for help. This works perfect. I can use a fix value in real project so i will no have sql injection. In anycase thank you very much for the link, i need to study this too... – Fabrizio Jun 08 '21 at 14:18
0

The simplest way, is to turn them into DateTime objects, then use format to get the result you are after. So something like:

$date = "2021-05-26";

$DT = DateTime::createFromFormat("Y-m-d", $date);
echo $DT -> format("m/d/Y");

This method also gives you an ability to do other conversions and/or modify the time etc.

DateTime create from format docs

DateTime format docs

Jamie Robinson
  • 832
  • 10
  • 16