Jump to:

5541 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Formatting Date

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1145 Views
  • eddieconnecti
    Avatar
    Community Member
    26 Posts

    Formatting Date Link to this post

    SS stores Datetime like '2008-06-21 20:30:00' not as timestamp in mySQL Database. I checked this with phpMyAdmin. If I use following code, it results in "01.01.1970", no matter what date I use. Does anyone have an idea how to get a dateobject out of this string? I would prefer not to use explode, I think there must be a php function to do it. I checked the sapphire api and they also use strtotime... No clue...

    Example of code:

    $starttime = strtotime( $this->start );
    $dateTip = date( 'd.m.Y', $starttime );

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Formatting Date Link to this post

    Well if your field is a SSDateTime field in the database when you do $this->dbObject('FieldName') in the PHP or $FieldName in the templates it returns the full object so you can call the various methods etc.

  • eddieconnecti
    Avatar
    Community Member
    26 Posts

    Re: Formatting Date Link to this post

    Sorry, doesn´t work. See, I use Datetime:

       public static $db = array(
          "Start" => "Datetime",
          "End" => "Datetime"
       );

    Should I better use SSDatetime? If I change the field, do I loose already stored dates? Do I have to write them all again?

    But I think, your solution with

    $starttime = $this->dbObject( "start" );

    should also work with MySQL Datetime...? Maybe I made a mistake, but where...

  • eddieconnecti
    Avatar
    Community Member
    26 Posts

    Re: Formatting Date Link to this post

    To close this thread I will tell you what finally did it:

    I changed DateTime to SSDateTime and left all other code as is. It works now. Best of it: I did not have to change one single date in the database.

    1145 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.