4

I'm working with dates and want to use Craft's Class reference to speed things along but am struggling to figure out how to use them. For example, this works as expected and returns the siteUrl:

return craft()->getSiteUrl();

But how do I format something more complicated? Say I want to format a date variable, how do I use any of these classes: https://craftcms.com/classreference/etc/dates/DateTime? I've tried all sorts but generally end up with:

Craft\WebApp and its behaviors do not have a method or closure named...
5

craft() is just a wrapper for the singleton instance of the WebApp class and you can call its methods directly like you're doing with getSiteUrl().

You'll notice that getSiteUrl() actually isn't in that list of WebApp methods, though. That's because it (along with other methods) live in AppBehavior, which is a Yii behavior and is very similar to a trait that gets it's methods "mixed-in" to the class that wants them (in this case, WebApp).

Stand-alone classes that are't attached to WebApp (craft()), like DateTime, you can instantiate like:

$dateTime = new DateTime();
$dateTime->format(...);

Or if they are static classes (like the Helpers), you can use directly like:

$lowerCaseString = StringHelper::toLowerCase('ABC');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.