refiking Posted April 21, 2008 Share Posted April 21, 2008 I am trying to change the format of my db stored date. I echoed both the $date variable and the $time variable to show you that the format I'm looking for is there, but it isn't giving the right date and time. Here is the code: while($ran = mysql_fetch_array($li)){ $lid = $ran['lid']; $date = $ran['dtime']; $time = date("m/d/y g:i A", $date); } Echo $date."<br>".$time; Here's what it returns: 2008-04-29 21:30:00 12/31/69 7:33 PM Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/ Share on other sites More sharing options...
hitman6003 Posted April 22, 2008 Share Posted April 22, 2008 Use MySQL's DATE_FORMAT function. The php date function expects a unix timestamp (seconds since epoc). However, MySQL is outputting a string representation of the date. You can either change the format in MySQL (using the DATE_FORMAT function), or you can use MySQL's UNIX_TIMESTAMP function to convert it to that format and then format the final in php. I recommend the former: SELECT DATE_FORMAT(dtime, "%m/%d/%y %g:%i %A") FROM table_name I'm not 100% sure that the format string is correct however (I didn't look up the format). http://www.mysql.com/date_format Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523451 Share on other sites More sharing options...
refiking Posted April 22, 2008 Author Share Posted April 22, 2008 Here is what I tried based on the link you provided: $dql = mysql_query('SELECT DATE_FORMAT(dtime, "%m/%d/%y") FROM dlg WHERE lname = "$lname"'); $dql2 = mysql_num_rows($dql); while ($run = mysql_fetch_assoc($dql)){ $dtime = $run['dtime']; //$time = date("m/d/y g:i A", $dtime); } Echo $dql2."<br>".$dtime; It returned 0 (zero). So it's not able to retrieve the data that way. Is there something I am missing or should take away? Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523466 Share on other sites More sharing options...
hitman6003 Posted April 22, 2008 Share Posted April 22, 2008 what is the data type of the field? Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523467 Share on other sites More sharing options...
refiking Posted April 22, 2008 Author Share Posted April 22, 2008 datetime Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523472 Share on other sites More sharing options...
hitman6003 Posted April 22, 2008 Share Posted April 22, 2008 First, by "it returned zero" do you mean it returned zero rows, or it failed? Second, your query is probably wrong...you have it enclosed in single quotes, which means it will not do variable substitution... change $dql = mysql_query('SELECT DATE_FORMAT(dtime, "%m/%d/%y") FROM dlg WHERE lname = "$lname"'); to $dql = mysql_query("SELECT DATE_FORMAT(dtime, '%m/%d/%y') FROM dlg WHERE lname = '$lname'"); Notice I changed double quotes to single, and single to double. Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523476 Share on other sites More sharing options...
refiking Posted April 22, 2008 Author Share Posted April 22, 2008 OK. Here's the code I put in: $dql = mysql_query("SELECT DATE_FORMAT(dtime, '%m/%d/%y') FROM dlg WHERE lname = '$lname'"); $dql2 = mysql_num_rows($dql); while ($run = mysql_fetch_assoc($dql)){ $dtime = $run['dtime']; } Echo $dql."<br>".$dql2."<br>".$dtime; Here's what it returned Resource id #4 1 Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523481 Share on other sites More sharing options...
craygo Posted April 22, 2008 Share Posted April 22, 2008 Hitman have you the mysql way to do it. if you want to do it with php, the date function requires the unix timestamp to give you the correct date. Since the date in mysql is a date and not a timestamp you have to convert it. $time = date("m/d/y g:i A", strtotime($dtime)); Ray Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523484 Share on other sites More sharing options...
craygo Posted April 22, 2008 Share Posted April 22, 2008 To use what you have above. $dql = mysql_query("SELECT DATE_FORMAT(dtime, '%m/%d/%y') AS dtime FROM dlg WHERE lname = '$lname'"); // add "AS dtime" $dql2 = mysql_num_rows($dql); while ($run = mysql_fetch_assoc($dql)){ $dtime = $run['dtime']; } Echo $dql."<br>".$dql2."<br>".$dtime; Ray Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523485 Share on other sites More sharing options...
hitman6003 Posted April 22, 2008 Share Posted April 22, 2008 $dg1 is a resource, so it being equal to "Resource id #4" is normal. $dg12 is the number of rows returned..."1" means one row was returned. You didn't get a value returned back for $dtime because mysql is returning the column name back with the name "DATE_FORMAT....". Either use a numeric index on your result... while ($row = mysql_fetch_array($dg1)) { $dtime = $row[0]; } Or, since you have one row, with one column, use mysql_result: $dtime = mysql_result($result, 0); Or assign the result column to a different name in the SQL query: SELECT DATE_FORMAT(dtime, '%m/%d/%y') AS dtime FROM dlg WHERE lname = '$lname' Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523486 Share on other sites More sharing options...
refiking Posted April 22, 2008 Author Share Posted April 22, 2008 Thanks Craygo and Hitman. I used the php version and it works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523487 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.