PHP DateTime Object Problem

Hi,

I'm having an issue and not sure if it's my code or a config issue on the server.  The server which the code is running has PHP Version 5.3.8.

When I run the below code, I don't get a return from the object oriented result.  However, when running the procedural code, I get '+2' for the result.

Any ideas on what the issue might be?

When I do a var_dump, I will get the date, but I won't be able to echo the value from the variable.

Thanks =)

//Procedural
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
echo "<br />";
//Object Oriented
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Open in new window

btuskeyAsked:
Who is Participating?
 
GaryCommented:
You don't have a default timezone set.

Add this to the page
date_default_timezone_set('Europe/Dublin');

and change for your locale.

(Or set it in your php.ini)
0
 
Dave BaldwinFixer of ProblemsCommented:
On hosting with PHP 5.3.24, I get the same response from both sections: +2 days

I also tried it on a XAMPP install with PHP 5.3.8 and got the same response.
0
 
btuskeyAuthor Commented:
Interesting...I'll speak with the systems engineers and ask them to confirm everything is installed and appropriately enabled.  I thought it should work and so when a simple code like that didn't return value for the object oriented, I wanted to confirm.

Thanks for the input Dave.  I'm going to leave this open and post the cause and solution once it's determined.

Best,
Bobby
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Ray PaseurCommented:
If you don't have a timezone set, you should get at least a Notice level message.  It's probably set in the INI file or something like that.

On my server with Linux and PHP 5.4+ I get correct output:
+2 days
+2 days

You might want to check the path to the script under test, or add some other kind of signal output to make certain you're testing the right script.
0
 
GaryCommented:
Doesn't return an error it actually gives you a 500 server error.
0
 
Dave BaldwinFixer of ProblemsCommented:
Not on my servers.  By the way, "date_diff" requires PHP 5.3 or greater.

Correction, on my MacBook with PHP 5.3.26 I got the error about the timezone.  It is likely that there are different default error reporting settings on the different servers.
0
 
GaryCommented:
PHP 5.3
Could be my server (nginx) returning a 500 code even tho I have report all errors on, still it is a timzone problem.
0
 
Dave BaldwinFixer of ProblemsCommented:
Could be.  I just tried it on two other web servers on Windows, one IIS with PHP 5.3.18 and one Apache with PHP 5.4.8.  Both showed the timezone error and both showed the "+2 days" response when it was corrected.  So it seems that the first two that I tried had the error reporting set differently than the rest.

I don't have nginx on any of my machines.
0
 
btuskeyAuthor Commented:
Thanks all for your input!

So, the timezone in the php.ini file is set as 'EDT', however, when I add...
date_default_timezone_set('America/New_York');

Open in new window


The function works as expected.  Looks like I should not have presumed 'EDT' would work when I echoed...
ini_get('date.timezone');

Open in new window

...and saw it set in the .ini file.

Thanks again to all, I appreciate it =)
0
 
Dave BaldwinFixer of ProblemsCommented:
Interesting.  The default time zone is set in 'php.ini' on all my servers so it must be the error reporting that is different.  You might want to set error_reporting on at the top of your PHP files so that it tells you about these things rather than failing silently.
error_reporting(E_ALL);

Open in new window

http://us3.php.net/manual/en/function.error-reporting.php
0
 
Ray PaseurCommented:
0
 
btuskeyAuthor Commented:
I've selected this answer as the best, however, I really appreciate everyone's input!

I've left the php.ini timezone set to 'EDT' and then just created an include file setting the timezone using 'date_default_timezone_set('America/New_York');'  and call the include file in the header include file.

The reason I've done this is, as much as I hated to do it, made the change in production and don't want any issues with any deprecated code (although I don't expect issues).  Once I have time, I'll go into the development environment and make the update globally before pushing it out to production.

Thanks again to all =)
0
 
Ray PaseurCommented:
I believe that the accepted solution does not relate to the issue.  If we are going to put this answer into the PAQ we need to see the demonstrated test case that shows how this answer fits into the context of the question.  Testing the solutions is a really good idea at Experts-Exchange because not all Experts test their solutions before they post!
0
 
Dave BaldwinFixer of ProblemsCommented:
I'm wondering a little bit too.  Everyone who posted in this thread told where they tested the code so that doesn't apply here.  It seems likely that the error reporting was not adequate in original situation.  We often see where people keep turning error reporting off rather than fix their errors.
0
 
GaryCommented:
I don't see how the error reporting level has anything to do with the question and the solution.
So what if he had error reporting turned off? I already said this does not necessarily report an  error and that the server may simply return a 500 error.
The simple fact is that setting a timezone solved the question - it is irrelevant that the server had EDT set as the timezone - this is nothing to do with the timezone of the PHP instance.
If it placates Ray I will ask the OP if I can reopen the question so that he can allocate 20 points for his blind link about PHP timezones (what has that got to do with the question) and 20 points to his other comment about him not working with the correct file (whatever!).

I take umbrage at Ray for his comment 'not all Experts test their solutions before they post'. If you are going to have a dig at me then do it directly and not in some indirect way. This was tested in a way as to replicate the OP's error and the relevant solution given!

Additionally the fact that he got EDT from echoing the timezone shows that there is no timezone set within php and it is relying on the server timezone (this would return the shorthand version of the timezone and not the full string if it was set)
0
 
Dave BaldwinFixer of ProblemsCommented:
It may not directly have anything to do with the question and the solution... but my concern is that it may be pointing to a larger problem in the code and on the servers that the asker is using.  I have no desire to take points away from you but I'll speak when I think I see a problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.