Excel Date to PHP date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sank06
    New Member
    • Dec 2006
    • 7

    Excel Date to PHP date

    for converting mm/dd/yy to yyyy-mm-dd, I used this code
    list($d, $m, $y) = preg_split('/\//', $normal_date);

    $mydate = sprintf('%4d%02 d%02d', $y, $m, $d);
    print $mydate;

    following date is not converted
    10/20/2011 ( that is to say, if both month and date are 2 digts)

    whereas rest of the excel dates are being converted
    2/12/2011
    6/10/2011

    pl help
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    use mktime function to convert to time. Then give that to the date function choosing the format that you want.

    Code:
    $mydate = date('Y-m-D',mktime(0,0,0,$d,$m,$y));

    Comment

    • pradeepkr13
      New Member
      • Aug 2010
      • 43

      #3
      For me it's working fine.

      Also, I agree with "dlite22", you should use PHP provided functions extensively to avoid any bugs and all.

      You can also thik of getting rid of regex preg_split() it slows down the execution. Use some string based functions to split it up

      Comment

      • pradeepkr13
        New Member
        • Aug 2010
        • 43

        #4
        @dlite22,
        I think it should be small d in place of capital D in date()

        Code:
        $mydate = date('Y-m-d',mktime(0,0,0,$d,$m,$y));

        Comment

        Working...