inspireddesign Posted October 11, 2009 Share Posted October 11, 2009 Hello everyone, I'm trying to extract just the date from a datetime stamp within the SQL syntax. When I run the sql it returns an error. The code is below. The error returned is as follows: Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in on line ... The statement runs fine without the convert() function within the sql statement. Of course. What could I be doing wrong? Thanks for any help on this one. <?php //making the query from table time_clock $Query = "SELECT * FROM time_clock JOIN employees ON time_clock.Emp_Id WHERE time_clock.Clockin BETWEEN convert(varchar(10),'2008-10-20', 101) AND convert(varchar(10),'2009-01-13', 101) AND time_clock.Emp_Id=53 "; $Result = mysql_query( $Query ); while( $Row = mysql_fetch_object( $Result ) ) { $output = $Row->Emp_Id." ". $Row->FirstName . " " . $Row->LastName . " ".$Row->Clockin." \n"; } die($output); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177336-solved-extracting-date-from-datetime-stamp-sql/ Share on other sites More sharing options...
inspireddesign Posted October 12, 2009 Author Share Posted October 12, 2009 Hello Everyone, I've figured out the correct syntax for my problem. Below in the corrected SQL statement. I've added an SQL function called DATE_FORMAT(). This will format the date per the format parameters indicating at the following url: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format The Clockin is the field I want to format in my database, then I pass those formatted dates to another field called FormattedDate and use that as my output. Hope this can useful to someone out there. <?php $Query = "SELECT *, DATE_FORMAT( Clockin, '%d-%m-%Y' ) AS FormattedDate FROM time_clock JOIN employees ON time_clock.Emp_Id WHERE time_clock.Clockin BETWEEN '2008-10-20' AND '2009-01-13' AND time_clock.Emp_Id=53 AND time_clock.LocationID=41 "; $Result = mysql_query( $Query ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177336-solved-extracting-date-from-datetime-stamp-sql/#findComment-935464 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.