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 4 of 4
  1. #1
    anu
    anu is offline
    New Coder
    Join Date
    Jan 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    calculating the difference between two date&time

    Hai,
    I am facing a problem while getting the difference between two dates in PHP. i used the following code
    $diff=date("Y-m-d",strtotime('01-01-2007') - strtotime('01-01-2008')) but it is returning wrong date. i tried with most of the functions provided with php but still i can't. also How it will possible if the date is having time also. please send us the code if anybody already knows.
    regards
    anu

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    What you're doing isn't going to return a date, its going to return an amount of time.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    anu
    anu is offline
    New Coder
    Join Date
    Jan 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so what is the solution for that. i want the differnce between two date with time. pl help me to sort out this

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    First, the format you are using is not valid for strtodate. You cannot use the hyphenated method directly with strtotime so instead use a slashed method: 01/01/2007.
    Second, using the date() function on the return value will result in 1 year after unix epoch (January 1, 1970 00:00 OR December 31, 1969 11:59), which is December 31, 1970 for this example.
    So, unless this is the result you want to use, your going to need to do some calculations:
    60 seconds per minute
    60 minutes per hour
    24 hours per day
    365.25 days per year
    Total real days per year is 31 557 600 seconds.

    Hope that helps you out!

    [edit]
    Thought I would throw this in too:
    32-bit systems don't linke negative times too much (especially windows) so you may want to consider using an absolute value for your difference calculations.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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