BaronGrivet BaronGrivet - 5 months ago 25
PHP Question

How to cast SilverStripe variable in Controller extension

I have the following code:

class Page_Controller extends ContentController
{
public function TimeNow() {
return date('Y-m-d H:i:s');
}
}


But the following template code doesn't work:

$TimeNow.Format(Ymd\THis)


I'm guessing this is because
TimeNow
isn't cast as an
SS_Datetime
object (which inherits the
Format
method).

How do I cast
TimeNow
as
SS_Datetime
?

Answer

Do this instead:

public function TimeNow() {
    return SS_DateTime::now();
}