Jump to content

[SOLVED] reading time in database


justinede

Recommended Posts

Hey when a user logs out, the time they logged out is added to my database, but in the database it just looks like a bunch of numbers.

 

Here is me logout script.

 

$time = time(); //// now... 
  
// mjr start - start session to get saved myusername 
session_start(); 
  
$sql = "UPDATE members SET timeout = '$time' WHERE username = '" . $_SESSION['myusername'] . "'"; 

mysql_query($sql)or die(mysql_error()); 
// mjr end 
  
session_unset(); 
  
session_destroy(); 
// Logged out, return home. 
Header("Location: index.php"); 
?> 

 

So anyone know how to make it into a time and date format?

Link to comment
https://forums.phpfreaks.com/topic/120909-solved-reading-time-in-database/
Share on other sites

It's because your using a timestamp. Change the "timeout" field type to datetime.

 

Then when you do your query, just use the NOW() function.

UPDATE members SET timeout = NOW() WHERE username = '" . $_SESSION['myusername'] . "'

Personally I would just make timeout DATETIME, then run the query like so,

 

$sql = "UPDATE members SET timeout = FROM_UNIXTIME(". time() .") WHERE username = '" . $_SESSION['myusername'] . "'";

 

 

The current date and time is now in the database in the form of YYYY-MM-DD HH:MM:SS

 

Then the query to select it would be something like this,

 

$query = mysql_query("SELECT *, UNIX_TIMESTAMP(timeout) AS timeout FROM members WHERE username = 'bleh'");

 

timeout is now a timestamp which you can format with date

 

$row = mysql_fetch_array($query);

echo date("m/d g:i a", $row['timeout']);

 

Would produce a string like 08/20 5:40 am

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.