-1

I have the following datetime 2014-02-05 17:12:48 stored in a php variable named $reportDb["reportDate"].

Now, It is in Y-m-d H:i:s format, I want it in d/m/Y H:i:s format. How can I set or reformat this variable?

I tried with datetime::createformat but It doesn't works.

apomene
  • 14,282
  • 9
  • 46
  • 72
araujophillips
  • 322
  • 1
  • 5
  • 22

3 Answers3

3
$formatted = date("d/m/Y H:i:s", strtotime($reportDb["reportDate"]))

If you want another method. Even if i would prefer the one already mentioned.

Output 05/02/2014 17:12:48

Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46
0

PHP 5.3 and older:

$dt = new DateTime('2014-02-05 17:12:48');
echo $dt->format(''d/m/Y H:i:s'');

PHP 5.4+:

$dt = (new DateTime('2014-02-05 17:12:48'))->format(''d/m/Y H:i:s'');

with the variable:

$dt = (new DateTime($reportDb["reportDate"]))->format(''d/m/Y H:i:s'');
John Conde
  • 217,595
  • 99
  • 455
  • 496
-1

If I understand you right, $reportDb["reportDate"] is already a DateTime object.

If so, all you need is ...

$reportDb["reportDate"]->format('d/m/Y H:i:s');

Cheers!

Hector Ordonez
  • 1,044
  • 1
  • 12
  • 20
  • It doesnt works and produce the following error: 'PHP Fatal error: Call to a member function format() on a non-object' – araujophillips Feb 06 '14 at 13:01
  • The $reportDb["reportDate"] has to be a DateTime object. PHP is telling you that it is NOT an object. Therefore, update your question because "datetime 2014-02-05 17:12:48" is not a DateTime, is a string. In that case John Conde's answer is the right one. – Hector Ordonez Feb 07 '14 at 15:45