Solved

PHP DateTime Object Problem

Posted on 2014-01-13
16
470 Views
Last Modified: 2014-01-14
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

0
Comment
Question by:btuskey
  • 6
  • 4
  • 3
  • +1
16 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
 

Author Comment

by:btuskey
Comment Utility
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
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Doesn't return an error it actually gives you a 500 server error.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:btuskey
Comment Utility
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
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
0
 

Author Closing Comment

by:btuskey
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now