slaterino Posted October 1, 2008 Share Posted October 1, 2008 Hi, I've created a php page which lists mysql date entries in the format "24 April 2009" for example. However, I now want there to be a possibility for multiple date entries such as "24-26 Apr 2009" or "24 Apr - 26 Apr 2009." To do this I have created an extra field in my database called $enddate but have played around with adding this extra date but with no luck so far. At the moment I have some code to affect how the date appears but can't work out how to use that code for two different fields. This is my current code for this: $query = "SELECT society, venue, dates, enddate FROM showdates where dates between CURRENT_DATE and CURRENT_DATE + interval 1 year ORDER BY dates ASC"; $result = mysql_query($query); while(list($society, $venue, $dates, $enddate)= mysql_fetch_row($result)) { list($year,$month,$day)=split("-",$dates); $dates=date("j M Y",mktime(0,0,0,$month,$day,$year)); echo ($dates?"<b>$dates</b> - ":"").($venue?"<b>$venue</b><br />":"").($society?"<b>$society</b><br />":"").("<br /><br />"); If anyone can give me any tips I would be really grateful!!!! Thanks! Russ Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 I think the strtotime() function will help you: http://us2.php.net/manual/en/function.strtotime.php Quote Link to comment Share on other sites More sharing options...
slaterino Posted October 1, 2008 Author Share Posted October 1, 2008 Thanks for the help, I've looked at that method as well as another which seems to work quite well as it means the MySQL is doing all the work. I am using: SELECT society, venue, date_format(dates, '%d %M %Y'), date_format(enddate, '%d %M %Y') which gives me possible results of "19 April 2009 - 20 April 2009" but does anyone know how I can combine these to give the results "19 - 20 April 2009" or "19 April - 20 April 2009." For instance, is there a way I can tell the first date to only show day and month if the second date is present? Thanks Russ Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 Mysql has an IF() statement that will let you do this. Quote Link to comment Share on other sites More sharing options...
slaterino Posted October 1, 2008 Author Share Posted October 1, 2008 Ah yes, I've used the IF() statement before but how could I use it so that start date appears as day, month and year but if end date is present the start date would only display day? Does that make sense? Is there a way of splitting the dates into day, month and year which would make this possible? Thanks Russ Quote Link to comment Share on other sites More sharing options...
Barand Posted October 1, 2008 Share Posted October 1, 2008 IF(MONTH(startdate) = MONTH(enddate) .... Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 1, 2008 Share Posted October 1, 2008 You will need to determine some more explicit rules on how you would display the date. "24-26 Apr 2009" or "24 Apr - 26 Apr 2009." Is fine if the date span does not go from Dec 31. to Jan. 1. query = "SELECT society, venue, dates, enddate FROM showdates where dates between CURRENT_DATE and CURRENT_DATE + interval 1 year ORDER BY dates ASC"; $result = mysql_query($query); while(list($society, $venue, $dates, $enddate)= mysql_fetch_row($result)) { list($start_year, $start_month, $start_day)=split("-",$dates); if (empty($enddate)) { //No end date. Show just the start date $dates = date('j M Y', mktime(0, 0, 0, $start_month, $start_day, $start_year)); } else { //There is an end date list($end_year, $end_month, $end_day)=split("-",$dates); $dates = $start_day.' '.date('M', $start_time); if ($start_year!=$end_year) { //Start and end have different years $dates .= " $start_year"; } $dates .= ' - ' . $end_day.' '.date('M', $end_time).' '.$end_year; } echo $dates; } //OUTPUT: // //One date: // 24 April 2009 //Two dates, same year: // 24 Apr - 13 Mar 2009 //Two dates, different year // 24 Dec 2009 - 3 Jan 2010 NOTE: Not tested, but the logic should be good. Quote Link to comment Share on other sites More sharing options...
slaterino Posted October 2, 2008 Author Share Posted October 2, 2008 Hi, Thanks so much for the help! Script worked perfectly except for two changes. I had to change $dates to $enddate in the one instance below: list($end_year, $end_month, $end_day)=split("-",$enddate); and also my empty date fields were not being stored in MySQL as null values but rather 0000-00-00, so I changed the IF() statement to: if ($enddate=="0000-00-00") Thanks for your help though. My understanding of PHP is slowly starting to improve! Russ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.