2

How would I convert the following so that it uses the datetime class?:

array(
    'post_date' => date('Y-m-d H:i:s', $date),
    'post_date_gmt' => gmdate('Y-m-d H:i:s', $date),
);

Eg this part is easy but how about the gmdate?

$oDate = new DateTime($date);
array(
    'post_date' => $oDate->format('Y-m-d H:i:s'),
    'post_date_gmt' => gmdate('Y-m-d H:i:s', $date),
);
John Magnolia
  • 16,769
  • 36
  • 159
  • 270

2 Answers2

3

Try this:

$oDate->setTimezone(new DateTimeZone('GMT'));
$oDate->format('Y-m-d H:i:s');
thelastshadow
  • 3,406
  • 3
  • 33
  • 36
1

In that case you need two object one for GMT.

$oDate = new DateTime($date);
$gmDate = new DateTime($date, new DateTimeZone('GMT'));

array(
   'post_date'     => $oDate->format('Y-m-d H:i:s'),
   'post_date_gmt' => $gmDate->format('Y-m-d H:i:s'),
);
Rikesh
  • 26,156
  • 14
  • 79
  • 87