0

I have a date time in string with format Y-m-d H:i:s like this:

$dateTime = '2018-07-06 18:53:21';

i want to conver it into dmy format, like this:

$convertedDateTime = $this->convertDateTime($dateTime);
echo $convertedDateTime; 

and the result i expected from above echo is 060718, how can i achieve this?

Tiny Dancer
  • 237
  • 5
  • 10

3 Answers3

3

You can use date_format like this:

echo date_format($date,"Y/m/d H:i:s");  //e.g. 2013/03/15 00:00:00
echo date_format($date,"dmy");   //e.g. 150313    -- Y capital would return 2013
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
2

there's two methods to use:

date - https://secure.php.net/manual/en/function.date.php

usage example:

$myDate = '2018-07-06 09:49:00';
$myDate = date('d-m-y', strtotime($myDate));

but more often than not you'll want to use DateTime https://secure.php.net/manual/en/class.datetime.php:

$myDate = DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 09:49:00');
$$newDate = $myDate->format('d-m-Y');
treyBake
  • 6,440
  • 6
  • 26
  • 57
1
\DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 18:53:21')->format('dmy');
mrf
  • 546
  • 3
  • 5