Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex to format a date for mysql... separated with hyphens 1

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB
i'm trying to format a date for mysql

the date is is currently 20100201 but i'd like to format it as 2010-02-01, separated with hyphens

what would be the ideal regex code to do this?

 
@feherke
I read Pushyr as saying (at marker 11 Feb 0645) that the universe of possible file names excluded any date separators. hence the substr solution.

I wholly agree that if there is an arbitrary separator to take account of then this must also be dealt with, most probably in a regex.

however the regex is complicated (not fatally so) by the new bit of information that immediately preceding the date pairs we can also have more digits.

so a pattern would be something like
Code:
$pattern = '/(\d{8})[.-]?(\d{8})\.csv$/i';
 
if it is an addition then preg_match is the way to go for sure. the pattern i posted above should work and is only a minor tweak to yours to deal with the variable prefixes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top