0

I have an array index that comes from an external API that returns a date in a string like this:

<?php echo gettype($meeting['date']); ?>

Results in "string".

This:

<?php echo $meeting['date']; ?>

Gives the following outcome: "2018-08-30".

I want to change the date format to "30-08-2018". How can i accomplish this?

El Klo
  • 173
  • 3
  • 14
  • parse the date with `DateTime` and format it to `d-m-Y` – Hudson Aug 30 '18 at 08:40
  • RTFM http://php.net/manual/en/datetime.format.php – daremachine Aug 30 '18 at 08:41
  • 1
    With all due respect, but questions like these have been answered with very good and definitive answers at least 50 times by now. Try to do some more research next time! (For example, here: https://stackoverflow.com/questions/2487921/convert-date-format-yyyy-mm-dd-dd-mm-yyyy) – Loek Aug 30 '18 at 08:43

4 Answers4

3

You can easily do this using DateTime class and the format() method:

// Create a DateTime instance
$date = new DateTime($meeting['date']);

// Format it:
echo $date->format('d-m-Y');

// Output: 30-08-2018
AymDev
  • 6,626
  • 4
  • 29
  • 52
3
$newDate = date("d-m-Y", strtotime($meeting['date']));

echo $newDate;

That's done.

AymDev
  • 6,626
  • 4
  • 29
  • 52
Eric
  • 336
  • 4
  • 17
2
$meeting['date']="2018-08-30";

echo date("d-m-Y", strtotime($meeting['date']));

The above code will do the job for you. It will get your string date and convert it to the format you need.

The output of the above code is : 30-08-2018

More about date function you can find here!!!

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
0

Use strtotime() and date():

$originalDate = "2018-08-30";
$newDate = date("d-m-Y", strtotime($originalDate));
SolveProblem
  • 83
  • 1
  • 9