Format date

Discussion in 'PHP' started by jupping, Oct 11, 2009.

  1. #1
    code :
    <body>
    <?
    date_default_timezone_set('Asia/Katmandu');
    $d = date("d/m/Y");
    $e =date("Y",$d);
    echo "$e";
    ?>
    </body>

    if i want to show $e = "2009" but when i run it's show "1970"
    i don't understand ,what's happen?
     
    jupping, Oct 11, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The date function is run on a timestamp, you are passing a string representation of a date instead. Doing so results in an invalid timestamp. The first timestamp is somewhere in 1970.

    So you can just do:

    
    $e = date('Y');
    
    PHP:
    Or:

    
    $e = date('Y', mktime(0, 0, 0, 10, 11, 2009));
    
    PHP:
     
    premiumscripts, Oct 11, 2009 IP
  3. jupping

    jupping Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if $e = select datetime from database
    and i want to show only year.
    How can i do?
     
    jupping, Oct 11, 2009 IP
  4. jupping

    jupping Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i write
    <?
    date_default_timezone_set('Asia/Katmandu');
    $d = date("d/m/Y");
    $e = date('Y',mktime($d));
    echo "$e";
    ?>

    It's good.Thank you very much
     
    jupping, Oct 11, 2009 IP
  5. EASYDOMAIN4ALL

    EASYDOMAIN4ALL Well-Known Member

    Messages:
    297
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    123
    #5
    shorten that
    
    
    date_default_timezone_set('Asia/Katmandu');
    echo date("Y");
    
    
    PHP:
    should be enough.
     
    EASYDOMAIN4ALL, Oct 12, 2009 IP