1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Show date difference in years

Discussion in 'MySQL' started by piropeator, Oct 21, 2016.

  1. #1
    Hi.
    I want to get the difference between two dates in years.
    I have this:
    SELECT DATEDIFF("2004-04-20","2010-05-7")
    Code (SQL):
    The result is shown in days. I need show this in years.
    Exist another way?
    Can some one help me?
    Thanks.
     
    Solved! View solution.
    Last edited: Oct 21, 2016
    piropeator, Oct 21, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Are you actually pulling these from a database?
     
    PoPSiCLe, Oct 21, 2016 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    I used this:
    $diff = $datetime1->diff($datetime2);
    
    Code (markup):
    Fields date are in a database.
     
    piropeator, Oct 31, 2016 IP
  4. #4
    Then just format it? Should be possible to just do:
    
    $diff = $datetime1->diff($datetime2)->format('Y');
    
    Code (markup):
     
    PoPSiCLe, Oct 31, 2016 IP
  5. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #5
    I use this. I guess it's the same.
    $diff = $datetime1->diff($datetime2);
    echo $diff->y
    PHP:
     
    piropeator, Oct 31, 2016 IP
  6. homemadejam

    homemadejam Well-Known Member

    Messages:
    143
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    145
    #6
    With the DATEDIFF function, you are able to select what format you want to result returned in.
    For example: DATEDIFF( datepart, start_date, end_date). You can replace the datepart variable with something like hour, day, week, year.
     
    homemadejam, Nov 1, 2016 IP