Round Posted December 14, 2006 Share Posted December 14, 2006 hello all,I have a field in my db that holds both date and time. e.g 14/12/2006 2.00pmI want to display just the time part on my page.How do I strip the date part away so I'm just left with 2.00pm??Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/30622-date-time/ Share on other sites More sharing options...
HuggieBear Posted December 14, 2006 Share Posted December 14, 2006 If you're using MySQL then just use the [color=green]DATE_FORMAT()[/color] function.[code]<?php// SQL statement to get date from database$sql = "SELECT DATE_FORMAT(date_column_name, '%h:%i %p') AS time FROM table_name";$result = mysql_query($sql);if (!$result){ echo mysql_error(); // If error, why couldn't we execute $sql}else { $time = mysql_result($result, 0, 0); // If success, assign the date to the variable $time}// Echo the timeecho $time;?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/30622-date-time/#findComment-141028 Share on other sites More sharing options...
Round Posted December 14, 2006 Author Share Posted December 14, 2006 cheers huggie, but I'm not sure thats going to work for meheres what i've got so far[code]$sql = "select * from events_table where location =\"$location\" "; #exe query $rs = mssql_query( $sql, $conn ) or die( "Could not execute Query"); #search for matches $num = mssql_num_rows ($rs); if ($num !=0) { $list = "<table align= \"center\" border=\"0\" cellpadding=\"4\" width=\"100%\">"; $list .= "<tr>"; $list .= "<th class=table>Whats on</th>"; $list .= "<th class=table>Location</th>"; $list .= "<th class=table>Booking code</th>"; $list .= "<th class=table>Planner</th>"; $list .= "<th class=table>Date & Time</th>"; $list .= "</tr>"; #retrieve data while ( $row = mssql_fetch_array( $rs ) ) { if ( $row["event_date"] != null) { $event_dt = date("l, jS F Y",strtotime($row["event_date"]))." ".$row["start_time"]; } else { $event_dt = "TBC"; } $list .= "<tr>"; $list .= "<td class=table>".$row["event"]."</td>"; $list .= "<td class=table>".$row["location"]."</td>"; $list .= "<td class=table>".$row["booking_code"]."</td>"; $list .= "<td class=table>".$row["planner_name"]."</td>"; $list .= "<td class=table>".$event_dt."</td>"; $list .= "</tr>"; } $list .= "</table>"; #list details if matches found or display no matches found msg #Below produces name etc at top of page echo( "<center><p><b><br>Here is the list of up an coming events"<br><br>"); echo( $list . "<br>" ); } else { echo ("<br><center>There are no events</center>"); } mssql_close ( $conn ); [/code]the problem is the field start_time has both a date and time in it, but the date is always incorrect. as i cant sift trough and remove this on the physical data in the db I was hoping to just strip it away. I wouldn't know where to start to insert another if statement. Quote Link to comment https://forums.phpfreaks.com/topic/30622-date-time/#findComment-141040 Share on other sites More sharing options...
HuggieBear Posted December 14, 2006 Share Posted December 14, 2006 I'm afraid I don't know how to do it with MSSQL, only MySQL.I've had a scout around, and I think you need to use the [color=green]convert()[/color] functionRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/30622-date-time/#findComment-141045 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.