Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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

This comment has been minimized.

Copy link

@j0k3r j0k3r commented Dec 9, 2013

What the purpose of get_object_vars?

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

@luxifer

This comment has been minimized.

Copy link

@luxifer luxifer commented Dec 9, 2013

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

@Pym

This comment has been minimized.

Copy link
Owner Author

@Pym 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

This comment has been minimized.

Copy link

@C-Duv 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
You can’t perform that action at this time.