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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

How to use and manipulate Dates


Go to End


3 Posts   1061 Views

Avatar
BuckeyeSam

Community Member, 7 Posts

12 June 2015 at 10:44am

When it comes down to it, I have no clue how to work with dates other than storing and retrieving them from a database. I'm trying to make a page that displays all the events for the week categorized by the day of the week. When It's all said and done it should look something like this:

Previous Week          Next Week
+------------------------------+
| Monday, June 8               |
+------------------------------+
 9:00am  - Event 1
 10:00am - Event 2
 
+------------------------------+
| Tuesday, June 9              |
+------------------------------+
 9:00am  - Event 3
 10:00am - Event 4

//...

+------------------------------+
| Sunday, June 14              |
+------------------------------+
 9:00am  - Event 13
 10:00am - Event 14

Here's what I have done so far:

I have this DataObject:

class Events extends DataObject {
    
    private static $db = array (
        'EventTitle' => 'Varchar',
        'EventDate' => 'Date',
        'EventTime' => 'Time'
    )
    //...

I have this php file:

//...
class CalendarPage_Controller extends Page_Controller {
    
    private $StartDate = //should default to today's date;
    
    private function SetStartDateToMonday() {
        //While $StartDate != Monday
            //$StartDate--
    }
    
    public function getDates() {
        SetStartDateToMonday();
        
        return array(
            //$StartDate,
            //$StartDate + 1,
            //$StartDate + 2,
            //$StartDate + 3,
            //$StartDate + 4,
            //$StartDate + 5,
            //$StartDate + 6
        )
    }
    
    public function getEvents($date) {
        return Events::get()
            ->filter(array(
                'EventDate' => $date  //I don't know if this will work or not
            ))
            ->sort(array(
                'EventTime' => 'asc'
            ));
    }
    
    public function PreviousWeek(SS_HTTPRequest $request) {
        //$startDate = $request->param('ID') - 1week
    }
    
    public function NextWeek(SS_HTTPRequest $request) {
        //$startDate = $request->param('ID') + 1week
    }
}

I have this template page:

//...
<a href="PreviousWeek/"<!-- This page's StartDate -->>Previous Week</a>
<a href="NextWeek/<!-- This page's StartDate -->">Next Week</a>
<% Loop getDates() %>
<h5><!-- $me variable if that is correct --></h5>
<table>
    <% Loop getEvents(<!-- $me -->) %>
    <tr>
        <td>$EventTime.Nice</td>
        <td>$EventTitle</td>
    </tr>
    <% End_Loop >
</table>
<% End_Loop >

As you can see my Controller class is a mess. I have a general idea how to make things work but I'm getting caught up in my lack of knowledge on PHP/Silverstripe functions and syntax. I've tried cutting it down to the bare bones to just make a list of dates using a start date of today and having no events and couldn't make that work. I've tried just listing events for today's date and that hasn't worked. I've been working on this for days looking at the Silverstripe API/documentation googling questions, and looking over Date() and DateTime() code from PHP, and event Date and SS_DateTime from Silverstripe and I'm flat out stuck and overwhelmed. I would love some input/ideas to help me out.

I know there are modules out there that I can use to accomplish what I want; but I'm still trying to learn the ins and outs of Silverstripe so that's why I'm going through all this fuss. Thanks for the help!

Avatar
BuckeyeSam

Community Member, 7 Posts

12 June 2015 at 10:51am

Edited: 12/06/2015 10:52am

To start things off simple I would like to be able to create this:

Monday, June 8
 
Tuesday, June 9

//...

Sunday, June 14

Here is my php file:

class CalendarPage_Controller extends Page_Controller {
    
    public function getDates() {
        $StartDate = SS_Datetime::now(); //Don't know if this is correct
        return array(
            $StartDate,
            //This is one place I'm stuck, how do I add to the date?
        )
    }
}

Here is my Template File:

<% Loop getDates() %>
<h5>$me<!-- Don't know if $me is correct here</h5>
<% End_Loop %>

Avatar
Pyromanik

Community Member, 419 Posts

12 June 2015 at 11:46pm

Where CalendarPage has_many Events:

<% loop $Events.GroupedBy('EventDate') %>
	<h3>$EventDate.Nice</h3>
	<ul>
		<% loop $Children %>
			<li>$EventTime - $EventTitle</li>
		<% end_loop %>
	</ul>
<% end_loop %>