Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts

    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 03:07 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •