Forum Moderators: coopster

Message Too Old, No Replies

preg_match mysql datetime

         

mexicoshanty

8:38 am on Oct 4, 2005 (gmt 0)

10+ Year Member



Hi there,

I'm having trouble trying to determine if a string is in the format of the mysql column type; datetime. The mysql datetime is in the format of:
year-month-day hour:minute:seconds

preg_match patterns are voodoo, too me anyway. Is there any experienced voodooists out there that can help me?

Thanks Coen

mexicoshanty

9:12 am on Oct 4, 2005 (gmt 0)

10+ Year Member



ok i think i solved it.
preg_match('#([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})#', $value)

if anyone can see an error in there, please reply

[edited by: jatar_k at 3:22 pm (utc) on Oct. 4, 2005]
[edit reason] disabled smilies [/edit]

coopster

8:02 pm on Oct 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You may want to assert a start (^) and end ($) of subject. Also, make sure the $value doesn't have any spaces surrounding it by trimming any off first:
$value = trim [php.net]($value); 
if (preg_match("/^\d{4}-\d{2}-\d{2} [0-2][0-3]:[0-5][0-9]:[0-5][0-9]$/", $value)) {
print "it's a match";
}

Lastly, just because the digits are there does not make it a valid date. You may want to actually validate the date portion of the $value before inserting the value into your database if that is indeed what you are doing here.

mexicoshanty

12:29 am on Oct 5, 2005 (gmt 0)

10+ Year Member



Thanks coopster, i like your way better :)