Skip to content

Instantly share code, notes, and snippets.

@Pym
Created December 9, 2013 11:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pym/7870904 to your computer and use it in GitHub Desktop.
Save Pym/7870904 to your computer and use it in GitHub Desktop.
<?php
$date = new \DateTime('now');
var_dump($date);
// object(DateTime)[594]
// public 'date' => string '2013-12-09 12:04:17' (length=19)
// public 'timezone_type' => int 3
// public 'timezone' => string 'Europe/Paris' (length=12)
var_dump($date->date);
// ContextErrorException: Notice: Undefined property: DateTime::$date
// Solutions :(
get_object_vars($date)['date'];
$date->format('Y-m-d H:i:s');
@j0k3r
Copy link

j0k3r commented Dec 9, 2013

What the purpose of get_object_vars?

It works great without: http://3v4l.org/vWOep

@luxifer
Copy link

luxifer commented Dec 9, 2013

he doesn't want to do a ->format() :D

@Pym
Copy link
Author

Pym commented Dec 9, 2013

@luxifer: 👍

@j0k3e: my DateTime object already contains a proper formatted date string, but for an obscure reason, I can't access it directly (even if its visibility is public). I was looking for a better way to access it than get_object_vars(), but it looks like that it's the best solution.

@C-Duv
Copy link

C-Duv commented Dec 11, 2013

@Pym: I don't get any ContextErrorException on var_dump($date->date); (PHP v5.5):

string '2013-12-11 21:19:46' (length=19)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment