• Date function in PHP

    Date() : Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time ().

    Syntax:

    1
    
    string date ( string $format [, int $timestamp = time() ] )

    Date function formatting:

    There are different types of formats that can be used in the date feature. Below is a summary of the variable used in date, and what each does.

    DAYS
    d – day of the month 2 digits (01-31)
    j – day of the month (1-31)
    D – 3 letter day (Mon – Sun)
    l – full name of day (Monday – Sunday)
    N – 1=Monday, 2=Tuesday, etc (1-7)
    S – suffix for date (st, nd, rd)
    w – 0=Sunday, 1=Monday (0-6)
    z – day of the year (1=365)

    WEEK
    W – week of the year (1-52)

    MONTH
    F – Full name of month (January – December)
    m – 2 digit month number (01-12)
    n – month number (1-12)
    M – 3 letter month (Jan – Dec)
    t – Days in the month (28-31)

    YEAR
    L – leap year (0 no, 1 yes)
    o – ISO-8601 year number (Ex. 1979, 2006)
    Y – four digit year (Ex. 1979, 2006)
    y – two digit year (Ex. 79, 06)

    TIME
    a – am or pm
    A – AM or PM
    B – Swatch Internet time (000 – 999)
    g – 12 hour (1-12)
    G – 24 hour c (0-23)
    h – 2 digit 12 hour (01-12)
    H – 2 digit 24 hour (00-23)
    i – 2 digit minutes (00-59)
    s 0 2 digit seconds (00-59)

    OTHER e – timezone (Ex: GMT, CST)
    I – daylight savings (1=yes, 0=no)
    O – offset GMT (Ex: 0200)
    Z – offset in seconds (-43200 – 43200)
    r – full RFC 2822 formatted date

    Example :

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    // Prints the day
    echo date("l") . "
    ";
     
    // Prints the day, date, month, year, time, AM or PM
    echo date("l jS \of F Y h:i:s A") . "
    ";
     
    // Prints October 3, 1975 was on a Friday
    echo "Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975)) . "
    ";
     
    // Use a constant in the format parameter
    echo date(DATE_RFC822) . "
    ";
     
    // prints something like: 1975-10-03T00:00:00+00:00
    echo date(DATE_ATOM,mktime(0,0,0,10,3,1975));
    ?>

    Result

    Thursday
    Thursday 31st of October 2013 03:19:09 AM
    Oct 3,1975 was on a Friday
    Thu, 31 Oct 13 03:19:09 -0400
    1975-10-03T00:00:00-04:00

Comments on this post

Leave a Reply

  • Security Code :


    eight × 1 =