Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

I need CMS DateField to only show date, not time.


Reply


2 Posts   1016 Views

Avatar
micahsheets

Community Member, 165 Posts

30 August 2009 at 11:59am

Edited: 30/08/2009 12:02pm

If I have a $db 'Date' => 'Date' and then in getCMSFields() I have new DateField('Date'), it shows the date and time. I want it to only show the date because there is no time needed.

Note: I just noticed that the Database field type was datetime so I changed it to date and that fixed the issue.

Avatar
Martijn

Community Member, 271 Posts

30 August 2009 at 1:32pm

Edited: 30/08/2009 1:36pm

But still sometimes you want to store date in datetime format, but in one situation only display them in DD-MM-YYYY and in another situation in DD-MM-YYYY HH:MM.

I couldn't find any SS build in solution, so I think creating a method in your controller will fix the issue:

function Nicedate(){
   $datetime = new DateTime($this->YourDateTimeField); // >= php 5.2
   return $datetime->format('jS, F Y'); // or d-m-Y
}