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
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts

    Stuck, how can you return JUST the last 2 digits of a year?

    Hi all. I have a function which I am writing which takes a datetime as its parameter, and I want it to return a 2 digit representation of year, but I can't figure out for the life of me how to do this! Can anybody tell me what I need to do with my code to get this to work? Thanks!

    This is what I have so far

    Code:
    function datetimeGetYear($datetime)
    { 
      $year = explode("-", $datetime); // 4 digit representation of year is now stored in $year[0]
      
      return $year[0]; // Stub... I need this to return a 2 digit representation of the year, not a 4 digit.
    }

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    PHP Code:
    function datetimeGetYear($datetime)
    {
        
    $year explode("-"$datetime);
        return 
    substr($year[0], -2);

    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    skcin7 (02-25-2010)

  • #3
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts
    Thank you!!! :-)

  • #4
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Or, you could use PHPs native functionality...

    PHP Code:
    $year_2d date("y",strtotime($datetime)); 


  •  

    Posting Permissions

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