Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP DateTime Object Problem

Posted on 2014-01-13
16
Medium Priority
?
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
16 Comments
 
LVL 84

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 2000 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
Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

 
LVL 111

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 84

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 84

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 84

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 111

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 111

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 84

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 84

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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

721 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