Jump to content

[SOLVED] Extracting Date from datetime stamp SQL


inspireddesign

Recommended Posts

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.  :confused:

 


<?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);

?>

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 );

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.