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 8 of 8
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    Subtracting date into numbers

    I am trying to take a date, and another date and subtract the number of days to make a number. So if you registered 01/01/08 and today is 01/05/08 it should subtract the dates and say 4 Days. So I want it to store the number of days they have been registered. Is there an easier way then this?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Use mktime to convert the dates to a unix timestamp then subtract the one from the other giving you the number of seconds difference, then multiply seconds by 60*60*24 to get days.

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Wow I am extremely confused. I know a very good amount of php, but I am beginning to learn date/time functions. Let me go to php.net and look at this mktime function. Thanks

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Assuming the date was in mm/dd/yy format you would:
    PHP Code:
    list($month$day$year) = explode('/'$date);
    $difference time() - mktime(000$month$day$year);
    $days floor($difference / (60 60 24)); 
    If you had the date stored in MySQL's DATE/DATETIME format you could use the MySQL date functions and it would be a lot easier.

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Yeah I have it stored in mysql in a DATE field. I just don't know how to change it to show days after 24 hours.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    The mysql datediff() function directly returns the number of days between two DATE or DATETIME values - http://dev.mysql.com/doc/refman/5.0/...ction_datediff

    No slow php code is necessary.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Thats useful and I read it over and over and can't understand. I am beginning to think im a failure with php. I try so hard to learn this complex stuff.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    This will return a column called "days_registered":
    Code:
    SELECT DATEDIFF(NOW(),`date`) AS `days_registered` FROM `table` WHERE 1;
    Replace "date" with the date field you're using.


  •  

    Posting Permissions

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