Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

This is a mess...I need to return a date...

Here's the scenario. I have within my URL the number of a week in year 2015. I want to use the setISO functionality to return a user friendly date.

I want to keep said function in a class, so here's what I'm thinking:

class DateCalc {
	
	public function arley_week($year, $week_number) {
	
		$date = new DateTime();
		$date->setISODate($year, $week_number);
		return $date;
	}	
	
}

Open in new window


...and then on my page, I've got this:

$anchor_week=new DateCalc;
$the_week=$anchor_week->arley_week(2015,2);
echo $the_week;

Open in new window


The error I get is Catchable fatal error: Object of class DateTime could not be converted to string in C:\wamp\www\SouthArea\GANT\index.php on line 6

I could use a procedural approach, but I want to use the OOP dynamic. Where am I blowing it?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could extend the DateTime class and add the magic method __toString(), but for my money, I would probably just use the procedural technique -- it's too easy and doesn't require any research.

Just curious... Are you sure you completely understand the PHP and ISO meanings of week numbers?  There are some quirky things about them that can produce very counter-intuitive results.  You might get a better result if you would describe the application needs and ask about best practices for handling the date / time values for the application.  Just a thought...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial