tonyawelch Posted May 19, 2009 Share Posted May 19, 2009 I have pulled out a DATE value from my mysql database from a column titled 'event_date' with value '2009-05-25' - using the following code: $get_events_query = "SELECT event_date FROM testevents WHERE YEAR(event_date) = '$year' AND MONTH(event_date) = '$month' "; $get_events = mysql_query($get_events_query) or die(mysql_error()); $event = mysql_fetch_array($get_events); The query runs fine, no errors. Now I have the date of 2009-05-25 that I want to format into 'May 25, 2009' to print. Easy enough...you would think. Used this code: $event_date = date_3_format($event[event_date]); function date_3_format($date) { $d = explode("-", $date); $date_formatted = date('F j, Y', mktime ($d[1],$d[2],$d[0])); return $date_formatted; } The result printed out "May 19, 2009" instead of "May 25, 2009", so I went and did some echoing out of variables to debug. echo $event[event_date]; //echos out 2009-05-25 as it should function date_3_format($date) { $d = explode("-", $date); echo $d[2]; //echos out "25" as it should $date_formatted = date('F j, Y', mktime ($d[1],$d[2],$d[0])); echo $date_formatted; //echos out "May 19, 2009" wtf? How did this happen? return $date_formatted; } I'm sure I've done something stupid somewhere but I can't even begin to imagine what it was. Thanks everyone for your help - I have found this forum to be the most helpful resource of PHP and MYSQL assistance anywhere on the net, truly. Link to comment https://forums.phpfreaks.com/topic/158783-solved-bizarre-date-error/ Share on other sites More sharing options...
JD* Posted May 19, 2009 Share Posted May 19, 2009 instead of breaking up the date, just try to do this: $date_formatted = date('F j, Y', strtotime($date)); See if that works for you (has for me in the past) Link to comment https://forums.phpfreaks.com/topic/158783-solved-bizarre-date-error/#findComment-837430 Share on other sites More sharing options...
tonyawelch Posted May 19, 2009 Author Share Posted May 19, 2009 That worked! And you rock! Of course, now I will wonder why the other way didn't work, but I'll worry about that later Link to comment https://forums.phpfreaks.com/topic/158783-solved-bizarre-date-error/#findComment-837442 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2009 Share Posted May 19, 2009 int mktime ([ int $hour= date("H") [, int $minute= date("i") [, int $second= date("s") [, int $month= date("n") [, int $day= date("j") [, int $year= date("Y") [, int $is_dst= -1 ]]]]]]] ) The parameters are hours, minutes, seconds, month, day, year. You are/were putting the month day and year into the hours, minutes, and seconds positions. Link to comment https://forums.phpfreaks.com/topic/158783-solved-bizarre-date-error/#findComment-837445 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.