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.
    SEMrush
     
    Solved! View solution.
    Last edited: Oct 21, 2016
    piropeator, Oct 21, 2016 IP
    SEMrush
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,626
    Likes Received:
    719
    Best Answers:
    151
    Trophy Points:
    420
    #2
    Are you actually pulling these from a database?
     
    PoPSiCLe, Oct 21, 2016 IP
  3. piropeator

    piropeator Active Member

    Messages:
    193
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #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 Active Member

    Messages:
    193
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #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 Active Member

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    65
    #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