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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Location
    Birmingham (UK)
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Extracting year from a date variable

    Hi Folks,

    I have an array of date variables $Gv_end_date[]

    I would like to extract the year into a separate variable but have hit a brick wall and am doing something quite wrong and no doubt stupid. I've put in a couple of echos with an exit and the output looks like the following. Any help pleasssse

    $endyear = date(Y,$Gv_end_date[$i]);
    echo $Gv_end_date[$i]; // displays 2015-07-22 (this is expected)
    echo $endyear; // displays 1970

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You can either split the year out via substring or explode, or convert it into a datetime and extract the year. I'd use a datetime, so I'll use strtotime.
    PHP Code:
    $date strtotime($Gv_end_date[$i]);
    $endyear date('Y'$date); 
    DateTime class can be used instead, but it does take a 5.3+ version of PHP to use it.

  • Users who have thanked Fou-Lu for this post:

    dambuster (04-15-2012)

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Location
    Birmingham (UK)
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thankyou Fou-Lu (can't believe it's that simple) very much appreciated.


  •  

    Posting Permissions

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