Solved

PHP DateTime Object Problem

Posted on 2014-01-13
16
475 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 83

Expert Comment

by:Dave Baldwin
ID: 39777405
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
ID: 39777443
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
ID: 39777454
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39777463
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
ID: 39777479
Doesn't return an error it actually gives you a 500 server error.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39777489
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
ID: 39777534
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 83

Expert Comment

by:Dave Baldwin
ID: 39777565
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
 

Author Comment

by:btuskey
ID: 39777763
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 83

Expert Comment

by:Dave Baldwin
ID: 39777823
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 109

Expert Comment

by:Ray Paseur
ID: 39777835
0
 

Author Closing Comment

by:btuskey
ID: 39778155
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 109

Expert Comment

by:Ray Paseur
ID: 39778189
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 83

Expert Comment

by:Dave Baldwin
ID: 39778214
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
ID: 39778315
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 83

Expert Comment

by:Dave Baldwin
ID: 39778587
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 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