Search code examples
phpphpactiverecord

PHP ActiveRecord automatically formatting date


So it seems that PHP ActiveRecord is formatting the update timestamp (for me anyway)

When I print the updated date, it displays:

Wed, 05 Jun 2013 21:14:48 +0200

Where as in the DB I've got:

2013-06-23 20:04:18

Just to be a 100% certain I used PDO to retrieve the record it displayed exactly the format I see in the DB.

Any idea why this automatic formatting might happen? and if there is a way to correct it?

Thanks.

EDIT

Ok, so I discovered how to manipulate it:

$records->updated->format('Y-m-d');

However, I'd still like to know why it happens and if there is a way to set it by default.


Solution

  • phpactiverecord retrieves it and stores it in an object that extends DateTime.

    Normally you could do ->format($yourformat) for any DateTime object, but for the phpactiverecord child you get a default format that gets used if you do not supply one.

    This extension also has a toString() function that calls this format(), so you get the default format (which you can set in this same class by the way).

    Take a look at the DateTime.php class provided by PHPActiveRecord to find out more, but this is what happens:

     class DateTime extends \DateTime{
        public static $DEFAULT_FORMAT = 'rfc2822';
        //array with formats here
    
        public function format($format=null){
            return parent::format(self::get_format($format));
        }
    
        public static function get_format($format=null){
            //get default format if nothing is provided
        }
    
        public function __toString(){
            //gets called when you use the object as a string
            return $this->format();
        }
    }