Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-03-2012, 01:52 PM   PM User | #1
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
DateTime function in PHP 5.2.17

I have this code in my script

PHP Code:
$date1 = new DateTime($Date['ori']['start']);
    
$date2 = new DateTime($Date['ori']['end']);
    
$interval $date1->diff($date2);
    
$daysDifference $interval->d
Which works on my localhost (PHP 5.3.1). However my web server only has PHP 5.2.17. Is there any other function i could use? All i need to do is get how many days difference there is between 2 dates.

EDIT: Turns out its just the diff function that does work. Fixed it by using

PHP Code:
$date1 = new DateTime($Date['ori']['start']);
$date2 = new DateTime($Date['ori']['end']);
$daysDifference round(abs($date2->format('U') - $date1->format('U')) / (60*60*24)); 

Last edited by tomharto; 02-03-2012 at 02:07 PM..
tomharto is offline   Reply With Quote
Old 02-03-2012, 02:36 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yeah, 5.3 introduced the DateInterval which is used with the datetime::diff, datetime::add and datetime::sub. I don't know why they didn't just make an extended datetime or through the burden statically to the dateinterval, but yeah just look for the DateInterval signature in any DateTime methods; those ones will all require 5.3+ to run.
Fou-Lu is offline   Reply With Quote
Old 04-05-2012, 02:23 PM   PM User | #3
gt3240
New to the CF scene

 
Join Date: Apr 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
gt3240 is an unknown quantity at this point
Quote:
Originally Posted by tomharto View Post
I have this code in my script

PHP Code:
$date1 = new DateTime($Date['ori']['start']);
    
$date2 = new DateTime($Date['ori']['end']);
    
$interval $date1->diff($date2);
    
$daysDifference $interval->d
Which works on my localhost (PHP 5.3.1). However my web server only has PHP 5.2.17. Is there any other function i could use? All i need to do is get how many days difference there is between 2 dates.

EDIT: Turns out its just the diff function that does work. Fixed it by using

PHP Code:
$date1 = new DateTime($Date['ori']['start']);
$date2 = new DateTime($Date['ori']['end']);
$daysDifference round(abs($date2->format('U') - $date1->format('U')) / (60*60*24)); 
Thanks for the tip, really helped me out. Can you tell me how to show up to the seconds?

Thanks in advance.
gt3240 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:33 AM.


Advertisement
Log in to turn off these ads.