Log in

View Full Version : Concatenate Dates



megs1328
02-05-2009, 04:47 PM
I currently have 3 columns: month, day, year. I would like to concatenate them in the WHERE clause like this:
CONCAT_WS('-', event.`year`, event.`month`, event.`day`) >= CURDATE()

so that I show only upcoming dates. the problem is, however, that if the month and day are single digits, there is no zero in front of them. so the string is coming out as

2009-4-5 >= 2009-04-05

Is there a way to add zeros to the single digits in the table itself or in the clause? When I try adding it in the table, it just deletes it. They are currently integers. I could make it a VARCHAR, but then it also depends on if the user enters it right. Is this even the best way to go about it? Thank you for your suggestions.

forum_amnesiac
04-14-2009, 01:38 PM
Hi

Sorry if this is a bit late and you have already sorted this problem out.

What I do to get the leading zeroes is to add 100 to each number and then use a string function to take the last 2 characters.

ie
$day1 = $day + 100
$day2=substr($day1,1,2)