fi0rini
fi0rini

Reputation: 1198

PHP Calendar Class with DateTime

I am trying to create a Calendar class that takes a new DateTime in its constructor or left blank to use the current time.

<?php
class Calendar
{
    public $dateTime = null;
    public function __construct($dateTime = new DateTime())
    {
        $this->dateTime = $dateTime;
        echo $this->dateTime->format('m-d-Y') . "\n";
    }
}
date_default_timezone_set('UTC');
$cal = new Calendar();
?>

but I keep getting this:

Parse error: parse error in /Users/nfior2/www/projects/companies/cortlandt/activities/cal.php on line 5

If I change line 5

public function __construct($dateTime = new DateTime())

to use int

public function __construct($dateTime=10)

and adjust the function __construct() body a bit without using DateTime::format... it works as I would expect.

Can anyone tell me what is going on here or how I can get to the bottom of this?

Upvotes: 1

Views: 1218

Answers (1)

user4004613
user4004613

Reputation:

You can try this code :

<?php
class Calendar
{
    public $dateTime = null;
    public function __construct($dateTime = null)
    {
        $this->dateTime = (isset($dateTime)? $dateTime : new DateTime());
        echo $this->dateTime->format('m-d-Y') . "\n";
    }
}
date_default_timezone_set('UTC');
$test = new DateTime('2000-01-01');

$cal1 = new Calendar($test);
$cal2 = new Calendar();
?>

Upvotes: 1

Related Questions