martin.kahil Posted November 18, 2007 Share Posted November 18, 2007 If I have a field in the MySQL database with contains a date. And I import it to a PHP variable like this $con = mysql_connect("localhost","","");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("db", $con);$result = mysql_query("select * from table");while($row = mysql_fetch_array($result)) { $date[$count]=$row['date']; } How do I get the day? For example if the date id 2007-11-18, I need the 18, How do I do this? Link to comment Share on other sites More sharing options...
Ingolme Posted November 18, 2007 Share Posted November 18, 2007 I have a function that takes dates from databases and returns values from it.This example will print the day onto the page: /* This is the function */function datetime($syntax,$datetime) { $year = substr($datetime,0,4); $month = substr($datetime,5,2); $day = substr($datetime,8,2); $hour = substr($datetime,11,2); $min = substr($datetime,14,2); $sec = substr($datetime,17,2); return date($syntax,mktime($hour,$min,$sec,$month,$day,$year));}/* Call the function and show the day of the given date */echo datetime("d",$row['date']); Link to comment Share on other sites More sharing options...
martin.kahil Posted November 19, 2007 Author Share Posted November 19, 2007 You mean like this? while($row = mysql_fetch_array($result)) { $date[$count]=datetime($row['date']); } And what is the $syntax? Link to comment Share on other sites More sharing options...
Ingolme Posted November 19, 2007 Share Posted November 19, 2007 No, the datetime function acts like the PHP date() function except with SQL results. That means you need to have a string before: datetime("Y",$row['date']); would return a four-digit year for example.In order to get the 18 of 2007-11-18, you write datetime("d",$row['date']); Link to comment Share on other sites More sharing options...
martin.kahil Posted November 19, 2007 Author Share Posted November 19, 2007 Ahhh.. I think i get it.. you simply "cut" parts of the string.. well I'll need to modify it, becaue I get the date like this "2007-11-17" so I need the function function datetime($syntax,$datetime) { $year = substr($datetime,0,4); $month = substr($datetime,5,2); $day = substr($datetime,8,2); return date($syntax,mktime(0,0,0,$month,$day,$year));}/// and callecho datetime("d",$row['date']); right? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.