puting a xxxxxxxx date in xx/xx/xxxx format

Discussion in 'PHP' started by Greenmethod, Jul 25, 2007.

  1. #1
    I am doing it with phone numbers (xxx.xxx.xxxx) and zip codes (xxxxx-xxxx) already, and i basically copied and pasted the script and made the changes so i'm not sure why its not working with the date..

    function Dates($sDate)
    {
    $sDate = ereg_replace("[^0-9]",'',$sDate);
    if(strlen($sDate) != 8) return(False);
    $sMonth = substr($sMonth,0,2);
    $sDay = substr($sDay,2,2);
    $sYear = substr($sYear,4,4);
    $sDate = $sMonth . "/" . $sDay . "/" . $sYear;
    return($sDate);
    }
    PHP:
    <? 
    if ($row['lic_dt'] > 0)
    	{
    	echo "This software package will expire on " . Dates($row['lic_dt']) . ".";
    	}
    	?>
    PHP:
    All the output shows is "This software package will expire on //.

    Thanks in advance for the help!
     
    Greenmethod, Jul 25, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    The error is here:
    
    $sMonth = substr($sMonth,0,2);
    $sDay = substr($sDay,2,2);
    $sYear = substr($sYear,4,4);
    
    PHP:
    In the substr() calls, none of the variables exists. Try replacing them all with $sDate.
     
    nico_swd, Jul 25, 2007 IP
  3. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ahhhhh... dang. I knew it was something easy.. mods can delete this thread if they want as it only makes me look like a simpleton :-D
     
    Greenmethod, Jul 25, 2007 IP
  4. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    But thanks so much for your help :)
     
    Greenmethod, Jul 25, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Btw, you can do the same with RegEx:
    
    function Dates($date)
    {
    	$date = preg_replace('/\D/', null, $date);
    	return strlen($date) == 8 ? preg_replace('/^(\d{2})(\d{2})(\d{4})$/', '$1/$2/$3', $date) : false;
    }
    
    PHP:
     
    nico_swd, Jul 25, 2007 IP
  6. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I wouldn't worry about it! We all have days like this :)

    Brew
     
    Brewster, Jul 25, 2007 IP
  7. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #7
    What is the $sDate that is being passed phrase as? Unixtime?

    
    function($sDate)
    {
      $sDate = strtotime($sDate);
      return  date('m/d/y',$sDate);
    }
    
    PHP:
     
    exodus, Jul 25, 2007 IP
  8. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No, it is just a number that i have in a database, like 01012007 or whatever.

    One more question on this.. I want to make an edit box that has three text fields, one for the month, date and year. To do this I need to separate the values in the database, which is already done with the dates() function. However, I can't figure out how to output the $sDay, $sMonth and $sYear. Any help would be great!

    Thanks,

    Nathan
     
    Greenmethod, Jul 26, 2007 IP