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?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
$the_week is not a string - it is an object - if you dump it you get something like this
DateTime Object
(
    [date] => 2015-01-05 21:09:36
    [timezone_type] => 3
    [timezone] => Africa/Johannesburg
)

Open in new window


So what you should be doing is

echo $the_week->date;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
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...
Ray PaseurCommented:
Here is some of my exploration of the PHP DateTime object and the week numbers.

Out-of-bounds week numbers are OK
Arrays and strings start with index zero, but week numbers apparently start with one
Week "one" may actually start in the prior year
The default for "day" is 1 == Monday, with Sunday == 0 (may affect traditional calendar algorithms)
The "day" is loosely typed; omitting the day == 1, passing NULL == 0
Everyone knows there are 52 weeks in a year, but there may be a week number 53
You can't just echo the date property - use DateTime::format() method with date() format strings

<?php // demo/temp_brucegust.php

/**
 * http://www.experts-exchange.com/questions/28714666/How-can-I-add-a-week-to-this-date.html
 * http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html
 * http://php.net/manual/en/datetime.setisodate.php
 * http://php.net/manual/en/datetime.format.php
 * http://php.net/manual/en/function.date.php
 */
error_reporting(E_ALL);
echo '<pre>';

// WE WILL USE THE OOP DATE OBJECT
$date = new DateTime();

// THESE ARE FIXED VALUES
$year = 2015;
$day  = 0; // SUNDAY

// SHOW SOME WEEK NUMBERS TO TEST
$weeks = [ -1, 0, 1, 2, 51, 52, 53, 54 ];

// MAKE THE TESTS AND REPORT THE RESULTS
foreach ($weeks as $week)
{
    $date->setISODate($year, $week, $day);
    $fmtd = $date->format('l, F jS, Y');
    echo PHP_EOL . "IN YEAR $year, WEEK $week STARTS ON $fmtd";
}

Open in new window

Outputs:
IN YEAR 2015, WEEK -1 STARTS ON Sunday, December 14th, 2014
IN YEAR 2015, WEEK 0 STARTS ON Sunday, December 21st, 2014
IN YEAR 2015, WEEK 1 STARTS ON Sunday, December 28th, 2014
IN YEAR 2015, WEEK 2 STARTS ON Sunday, January 4th, 2015
IN YEAR 2015, WEEK 51 STARTS ON Sunday, December 13th, 2015
IN YEAR 2015, WEEK 52 STARTS ON Sunday, December 20th, 2015
IN YEAR 2015, WEEK 53 STARTS ON Sunday, December 27th, 2015
IN YEAR 2015, WEEK 54 STARTS ON Sunday, January 3rd, 2016

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.