0

I need to write/read the date and time from the DB table. When I log in to my page, I wnat to read the saved date and time and when I logout I want to write to the DB the curent date and time, so When I login next time, to read the last login date and time. I stored a date format like this in the DB table "2014-09-12 14:48:30" but my code is not even read from it..

$login_event = mysql_query("SELECT DATE_FORMAT(events, '%M %D, %Y') AS mydate FROM login_events WHERE name = $login_name");
echo $login_name;

Please if someone can help me out on this I will be very grateful!

styven
  • 51
  • 1
  • 9

2 Answers2

0
$login_event = mysql_query("SELECT DATE_FORMAT(events, '%M %D, %Y') AS mydate FROM login_events WHERE name = '$login_name'");

$row = mysql_fetch_array($login_event);

echo $row['mydate'];
mariobgr
  • 2,143
  • 2
  • 16
  • 31
  • thank you very much for the suggestions, but it give me the `mysql_fetch_array(): supplied argument is not a valid MySQL` error... – styven Sep 12 '14 at 12:06
  • thank you!! working like a charm! only a little think, it's display only the date... the time is not displayed... – styven Sep 12 '14 at 12:11
  • Then you need to put some more arguments ad the second parameter in DATE_FORMAT. I guess you'll wand something like ... **DATE_FORMAT(events,'%M %D, %Y %T')** ... – mariobgr Sep 12 '14 at 12:14
  • yes, thank you! Now it's displayed correctly! And when I log out, how to udpate this date and time on the DB table? – styven Sep 12 '14 at 12:21
  • when you logout, on logout PHP perform an UPDATE query and set events = date('Y-m-d H:i:s') (this is php code) where name='$login_name' – mariobgr Sep 12 '14 at 12:26
  • I try this code, but is not updating: `mysql_query("UPDATE $tablename SET events = date('Y-m-d H:i:s') WHERE name = '$login_name'");` do you have any suggestion? – styven Sep 12 '14 at 12:39
  • mysql_query("UPDATE $tablename SET events = '" . date('Y-m-d H:i:s') ."' WHERE name = '$login_name'"); – mariobgr Sep 12 '14 at 12:39
  • yes, you tell me that is a PHP code... sorry. It's working now! Thank you very much for your help! Only one last thing, I don't know what time zone is used, because is with 2 housr behind from my time?! – styven Sep 12 '14 at 12:44
  • no problem! I resolved! the code is `date_default_timezone_set("America/New_York");` Thank you again! – styven Sep 12 '14 at 12:57
0

You have your mysql_query() syntax wrong, check here: http://www.w3schools.com/php/func_mysql_query.asp (unless you just rewrote it from head).

Plus you have to fetch the array: http://www.w3schools.com/php/func_mysql_fetch_array.asp

$result = mysql_fetch_array($login_event);
echo $result["mydate"];
whiteestee
  • 301
  • 1
  • 4
  • 12