extract the year from a full date string

Posted on 2004-11-07
Last Modified: 2007-12-19
in my data base i have a field which contains the date that the user joined. once i have taken that out of the database i want to create a new string which is the year from the other value. how would i do that.

ps. i only need to know how to do this

date user joined - 05/08/2004 10:26:23
from that  ^ get this: year user joined - 2004
Question by:jonnyfb
    LVL 3

    Expert Comment

    Hi jonnyfb,

    what languages can u use on the server? ASP/php/something else?
    is the field in the database a date field?
    LVL 1

    Expert Comment

    If you're extracting the date from the database, you can grab the year value at that time in many databases.

    MySQL example:

    SELECT YEAR(Date_Field) FROM My_Table

    If you're relegated to performing it in your script, however, this is how you could do it in PHP:

    $my_date = '05/08/2004 10:26:23';

    // divide date up into date and time
    $arrDateTime = explode(' ', $my_date);

    // divide date into component parts
    $arrDate = explode('/', $arrDateTime[0]);

    // display year
    print $arrDate[2];

    LVL 1

    Author Comment

    im using ASP and i dont need help gettin the value out of the database. i need to seperate the year from a full date
    (eg. i need to get 2004 out of 05/08/2004 10:26:23)
    LVL 1

    Accepted Solution

    You should be able to do the same thing I described in php (note: this is untested code).

    my_date = "05/08/2004 10:26:23"

    ' divide date up into date and time
    arrDateTime = split(my_date, " ")

    ' divide date into component parts
    arrDate = split(arrDateTime(0), "/")

    ' display year
    Response.Write arrDate(2)
    LVL 1

    Author Comment

    ok ill try it

    ps. Quick answer thanks!
    LVL 1

    Author Comment

    ok that works! Thanks! Split is really useful!
    LVL 3

    Expert Comment

    asp and wsh both use vbscript as their core language.

    check out the vbscript language reference

    especially the function list,  knowing what built in functions  exist  even if you dont know how to use them is pretty darn handy

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now