Solved

PHP DateTime Object Problem

Posted on 2014-01-13
16
485 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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 110

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 110

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

626 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