pietbez Posted February 26, 2008 Share Posted February 26, 2008 my date format on db used to be "26.02.2008" i have changed it to "2008.02.26" so that i can cronologicaly sort by date. everything works fine exept for one of my php csripts in my calendar that reads from this table. this script now gives no output at all. can anyone please give me some pointers? <? include('functions.php'); $year=$_POST['year']; $month=$_POST['month']; $sql = "SELECT event_date FROM calendar_events WHERE status='confirmed'and event_type='gig'"; $result = mysql_query($sql); $x=mysql_num_rows($result); $postdata=''; if ($x>0) { for ($i=0;$i<$x;++$i) { $row=mysql_fetch_row($result); $data=$row[0]; $len=strlen($data); if ($len==9) $data='0'.$data; $monthpoint=strpos($data,'.'); $checkmonth=substr($data,$monthpoint+1,2); $checkyear=substr($data,$monthpoint+4); if ($year==$checkyear && $month==$checkmonth) if ($postdata=='') $postdata=$postdata.$data; else $postdata=$postdata.' '.$data; ;} ;} echo "resultstr=".$postdata; ?> Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 hmm strange code you have. I would use this: Check out if it works <? include('functions.php'); $year=$_POST['year']; $month=$_POST['month']; $sql = "SELECT event_date FROM calendar_events WHERE status='confirmed'and event_type='gig'"; $result = mysql_query($sql); $x=mysql_num_rows($result); $postdata=''; if ($x>0) { for ($i=0;$i<$x;++$i) { $row=mysql_fetch_row($result); $fr_date = explode('.', $row[0]); $checkmonth=$fr_date[1]; $checkyear=$fr_date[0]; if ($year==$checkyear && $month==$checkmonth) if ($postdata=='') $postdata=$postdata.$data; else $postdata=$postdata.' '.$data; ;} ;} echo "resultstr=".$postdata; ?> Quote Link to comment Share on other sites More sharing options...
pietbez Posted February 26, 2008 Author Share Posted February 26, 2008 thanks freenity but it still gives me no output all i get as output is resultstr= http://www.dorp.co.uk/gigs/test.php Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted February 26, 2008 Share Posted February 26, 2008 why not just use the mysql date time format so you can take advantage of all its power Quote Link to comment Share on other sites More sharing options...
pietbez Posted February 26, 2008 Author Share Posted February 26, 2008 to be honnest, i dont know how to use date time format. but now i am trying a whole new aproach. dont laugh at my code, i am bran new at this this gives me output. now all i have to do is flip the dates around so it reads dd.mm.yyyy so my flash file can read it. how do i use explode?? <? include('../qazwsxedc/config.php'); mysql_connect($server,$username,$password); @mysql_select_db($database) or die ("Unable to connect to the database"); $year=$_POST['year']; $month=$_POST['month']; $min="$year".'.'."$month".'.'.'00'; $max="$year".'.'."$month".'.'.'32'; $do=mysql_query ("SELECT event_date FROM calendar_events WHERE event_date>'$min' and event_date<'$max' and status='confirmed'and event_type='gig' "); $x=mysql_num_rows($do); if ($x>0) { while ($row = mysql_fetch_array($do, MYSQL_ASSOC)) { $name.=$row["event_date"]; $name.=' '; } ;} echo "resultstr=".$name; ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted February 26, 2008 Share Posted February 26, 2008 take a good look at the date() function http://w3schools.com/php/func_date_date.asp that is a good list of how to format a date perfectly. mysql likes the date in "YYYY-MM-DD HH:MM:ss:ms" <?php #mysql date in php $date = date("Y-m-d G:i:s"); ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 yes, but he seems to be using a varchar for the date. pietbez, the best answer is to change the datatype of that field to DATE. More can be read on that here: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html for an easily solution working with what you have, you can use: while ($row = mysql_fetch_array($do, MYSQL_ASSOC)) { list($year,$month,$day) = explode('.',$row["event_date"],3); $name.="$day.$month.$year "; } Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 to be honnest, i dont know how to use date time format. but now i am trying a whole new aproach. dont laugh at my code, i am bran new at this this gives me output. now all i have to do is flip the dates around so it reads dd.mm.yyyy so my flash file can read it. how do i use explode?? <? include('../qazwsxedc/config.php'); mysql_connect($server,$username,$password); @mysql_select_db($database) or die ("Unable to connect to the database"); $year=$_POST['year']; $month=$_POST['month']; $min="$year".'.'."$month".'.'.'00'; $max="$year".'.'."$month".'.'.'32'; $do=mysql_query ("SELECT event_date FROM calendar_events WHERE event_date>'$min' and event_date<'$max' and status='confirmed'and event_type='gig' "); $x=mysql_num_rows($do); if ($x>0) { while ($row = mysql_fetch_array($do, MYSQL_ASSOC)) { $name.=$row["event_date"]; $name.=' '; } ;} echo "resultstr=".$name; ?> explode() http://ar2.php.net/manual/en/function.explode.php Well with the code you provided that gave you output all you have to do is: $dates = explode(".",$row["event_date"]); that will give you an array $dates with month, day and year. That will look: $dates[0] = year $dates[1] = month $dates[2] = day you can see it doing print_r($dates); just after making an explode; 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.