robcrozier Posted November 14, 2007 Share Posted November 14, 2007 HI all, i wonder if someone can help me with this. Im not very good with regular expressions but i would like to use the ereg() function to check for a date in this format: yyyy/mm/dd. any help appreciated! Cheers all! Link to comment https://forums.phpfreaks.com/topic/77350-regular-expressions-help-date-format/ Share on other sites More sharing options...
dbo Posted November 14, 2007 Share Posted November 14, 2007 This is the wrong forum. But the regex is: /^[0-9]{4}\/[0-9]{2}\/[0-9]{2}$/ To verify ranges and what not you'll need more than this though. That just validates the format. yyyy/mm/dd Link to comment https://forums.phpfreaks.com/topic/77350-regular-expressions-help-date-format/#findComment-391593 Share on other sites More sharing options...
robcrozier Posted November 14, 2007 Author Share Posted November 14, 2007 sorry about the wrong forum but that doesn't seem to work? Link to comment https://forums.phpfreaks.com/topic/77350-regular-expressions-help-date-format/#findComment-391608 Share on other sites More sharing options...
dbo Posted November 14, 2007 Share Posted November 14, 2007 Define doesn't work. Link to comment https://forums.phpfreaks.com/topic/77350-regular-expressions-help-date-format/#findComment-391673 Share on other sites More sharing options...
BenInBlack Posted November 25, 2007 Share Posted November 25, 2007 I would say that dbo's regex is proper, but since you said you wanted it to work with ereg modified his code to wrap the items in () parens to use with preg_replace $output = preg_replace(/^([0-9]{4})\/([0-9]{1,2})\/([0-9]{1,2})$/,*,1977/1/1); to use with ereg, if (ereg ("([0-9]{4})\/([0-9]{1,2})\/([0-9]{1,2})", $date, $regs)) { echo "$regs[3].$regs[2].$regs[1]"; } else { echo "Invalid date format: $date"; } Link to comment https://forums.phpfreaks.com/topic/77350-regular-expressions-help-date-format/#findComment-398638 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.