SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Thinks There is 6 Days When There is 7

    Doing a date difference between 24th March 2008 and 31st March 2008.

    If you look on a calendar, then there are 7 days between the 2 dates.

    Do it through a standard PHP timestamp sum and it only shows 6 days.

    Here's the script:
    PHP Code:
    $d1=mktime(0,0,0,03,24,2008);
    $d2=mktime(0,0,0,03,31,2008);
    number_days floor(($d2-$d1)/86400); 
    It actually shows 6.9583333333333 if I don't put the floor() in, but I need that in for other days.

    Anyone else come across this problem?

    Ian

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by spoondevil View Post
    If you look on a calendar, then there are 7 days between the 2 dates.
    no, that's not true

    between the 24th and 31st are only the 25th, 26th, 27th, 28th, 29th, and 30th

    six
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    no, that's not true

    between the 24th and 31st are only the 25th, 26th, 27th, 28th, 29th, and 30th

    six
    LOL, you got me on that one...

    What I should've said was between 00:00:00AM on the 24th March 2008 and 00:00:00AM on the 31st March 2008 there are 168 hours which breaks down to 7 days, but PHP is only recognising it as 6.95 days which throws my query into the ground

  4. #4
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    r937 is spot on. However, you may use ceil() to make that 6 go up to 7 if you insist there is 7 days.
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    just out of curiosity, would you kindly echo the values you get for $d1 and $d2
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have come across this before, twice. The first time I din't know what happend (was in march), and I couldn't duplicate the problem.

    Next time it happend thou, I dug a little deeper. Has to do with daylight savings time, DST. Check out php.net/mktime for more info.

  7. #7
    SitePoint Enthusiast jagat_21's Avatar
    Join Date
    Jun 2007
    Location
    Ahmedabad,India
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    just out of curiosity, would you kindly echo the values you get for $d1 and $d2
    Following is the output :

    1206297000 <== D1 Value
    1206901800 <== D2 Value
    7 <== No. Of days.

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    well, i don't have php, so i ran those dates through the mysql UNIX_TIMESTAMP function, and i got two completely different values!!!!

    2008-03-31 = 1206936000
    2008-03-24 = 1206331200

    difference = 604800 seconds = 7.0 days exactly
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Enthusiast jagat_21's Avatar
    Join Date
    Jun 2007
    Location
    Ahmedabad,India
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    this might help u. Check it if suits ur requirement.....

    Code PHP:
    /* Function For Date Difference */ 
    	function dateDiff($dformat, $endDate, $beginDate)
    	{
    		$date_parts1=explode($dformat, $beginDate);
    		$date_parts2=explode($dformat, $endDate);
    		$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    		$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    		return $end_date - $start_date;
    	}
        $date1="03/24/2008"; 
        $date2="03/31/2008";
        print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";

    Will generate following output :

    If we minus 03/24/2008 from 03/31/2008 we get 7.


  10. #10
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    just out of curiosity, would you kindly echo the values you get for $d1 and $d2
    PHP Code:
    $d1=1206316800
    $d2
    =1206918000 

  11. #11
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    well, i don't have php, so i....
    No wonder you do all your date formatting in MySQL then

  12. #12
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Off Topic:

    actually, no -- i do all my date formatting in ColdFusion
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  13. #13
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ickmund View Post
    Next time it happend thou, I dug a little deeper. Has to do with daylight savings time, DST. Check out php.net/mktime for more info.
    This sounds like the logical explanation as the BST +1hr comes in on the 25th March in 2008, so it might be that that's throwing a spanner into the works.

    But, as the date of BST changing every year as it's always on a Saturday night rather than a specific date, I think I'm gonna have to go with jagat_21's code as that seems to work no matter what year/date you are looking at.

  14. #14
    SitePoint Enthusiast lkagan's Avatar
    Join Date
    Sep 2007
    Location
    Boca Raton, Florida
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jagat_21's code is a solution I haven't seen before and a good one at that. As for the people that didn't know about the conversion functions jagat_21 used (like myself), we could use PEAR's Date and Date_Span.
    Larry Kagan
    Lead Web Application Developer
    Superiocity, Inc.

  15. #15
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    strtotime is your friend.
    http://www.php.net/manual/en/function.strtotime.php

    PHP Code:
    <?php

    $date 
    '03/24/2008';

    $dA strtotime($date);
    $dB strtotime($date ' +7Days');

    var_dump($dA);
    var_dump($dB);

    var_dump(date('F j, Y'$dA));
    var_dump(date('F j, Y'$dB)); // 03/31/2008
    // n
    Edit:
    Also found this.
    PHP Code:
    $d1 strtotime('03/24/2008');
    $d2 strtotime('03/31/2008');

    echo 
    'Hours difference = '   floor(($d2 $d1) / 3600) . '<br>';
    echo 
    'Minutes difference = ' floor(($d2 $d1) / 60)   . '<br>';
    echo 
    'Seconds difference = ' . ($d2 $d1)               . '<br>';

    echo 
    'Month difference = ' floor(($d2 $d1) / 2628000)  . '<br>';
    echo 
    'Days difference = '  floor(($d2 $d1) / 86400)    . '<br>';
    echo 
    'Year difference = '  floor(($d2 $d1) / 31536000) . '<br>'
    returns:
    Code:
    Hours difference = 168
    Minutes difference = 10080
    Seconds difference = 604800
    Month difference = 0
    Days difference = 7
    Year difference = 0
    May help.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  16. #16
    SitePoint Enthusiast lkagan's Avatar
    Join Date
    Sep 2007
    Location
    Boca Raton, Florida
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remember a caveat to using strtotime(), it is based on the UNIX epoch, 1/1/1970. Passing any dates before that as a parameter will give you a negative number of seconds which still works for calculations. Just don't try to pass the value back to date() or you'll always get 12/31/1969.
    Larry Kagan
    Lead Web Application Developer
    Superiocity, Inc.


Bookmarks

Posting Permissions

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