Small date parsing issue

Discussion in 'PHP' started by blackburn2413, Oct 13, 2010.

  1. #1
    Hey everyone, did some research to try to figure out the issue, but still having issues. Hoping for some help.

    I started my reading here : http://php.net/manual/en/function.strtotime.php

    Basically I have a string variable that is an 8 digit number: ex- MMDDYYYY

    All I want to accomplish is taking the variable $date and displaying it in plain text so it would work like this:

    $date = "10142010"

    when I use print or echo on my page, I want that variable to display as October 10, 2010.


    Any pointers?
     
    blackburn2413, Oct 13, 2010 IP
  2. ramsarvan

    ramsarvan Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    hi,
    your variable, $date = 10142010;

    Now seperate the first two digits by using,

    $rest = substr($date, 0, -6);
    $dateandyear = substr($date,-6);

    it will returns the first two digits,

    place a if() statement,

    if($rest == 01) {

    $rest = January;
    }
    else if($rest == 02) {

    $rest = Feb;
    }
    .
    .
    .
    repeate it for all 12 months,

    now echo $rest.'&nbsp'. $dateandyear;

    the output will be,
    October 10 2010
     
    ramsarvan, Oct 13, 2010 IP
  3. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    hey, the following shall sort it neatly..

    
    $date = '10142010';
    
    $month = substr($date, 0, 2);
    $day = substr($date, 2, 2);
    $year = substr($date, 4, 4);
    
    $new_date = date('F, d Y', mktime(0, 0, 0, $month, $day, $year));
    
    PHP:
     
    mastermunj, Oct 14, 2010 IP
  4. blackburn2413

    blackburn2413 Member

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    EDIT:

    I retract my previous post- I didnt know I received another reply already with the needed info.

    Thanks mastermunj!
     
    blackburn2413, Oct 16, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    $date = '10142010';
    
    preg_match('#^(?P<month>\d{2})(?P<day>\d{2})(?P<year>\d{4})$#D', $date, $digits);
    
    $timestamp = strtotime("{$digits['month']}/{$digits['day']}/{$digits['year']}");
    
    echo date('F, d Y', $timestamp);
    PHP:
     
    Last edited: Oct 17, 2010
    danx10, Oct 17, 2010 IP