Jump to content

Recommended Posts

Currently I have every time a user logs in the datetime saved into the database.

 

I want to extract this datatime and echo it in a friendly format. This is currently saving the current datetime to the lastlogin field

 

This is how I am loading the datetime from the database:

 

$logincheck = "SELECT * FROM guest WHERE username = '$activeusername' AND password = '$activepassword'";
//echo $query;
$result = mysql_query($logincheck);

while($r=mysql_fetch_array($result)) 
{ 
$admin=$r["admin"];
$lastlogin=$r["lastlogin"];
$name=$r["name"];
}

 

and setting current datetime back to database:

 

$date = date("m-d-y H:i:s");
$sql = "UPDATE guest SET lastlogin='$date' WHERE username='$activeusername'";
echo $sql;
$updatelogin = mysql_query($sql) or die(mysql_error());
if($admin == Yes) {

 

echo "Your last login was: ".$lastlogin;

 

gives: Your last login was: 2005-11-08 19:14:55 <-- why is this date wrong?

 

How would I extract parts from date time and echo something like:

 

Your last login was on 11th May 2008 at 19:14

 

 

I din't think it would work with a simple date function :o!

I made it really hard on myself I guess :s

 

<?php

function split_date ($dateTime)
{
$splitTime ['year'] = substr ($dateTime,0,4);
$splitTime ['month'] = substr ($dateTime,5,2);
$splitTime ['day'] = substr ($dateTime,8,2);

$dateTimeLength = strlen ($dateTime);

if ($dateTimeLength == 19)
{
	$splitTime ['hour'] = substr ($dateTime,11,2);
	$splitTime ['minute'] = substr ($dateTime,14,2);
	$splitTime ['second'] = substr ($dateTime,17,2);	
}

return $splitTime;
}

function format_date ($dateTime, $request)
{
$splitTime = split_date($dateTime);
$postTime['date'] = $splitTime ['day'] . "/" . $splitTime ['month'] . "/" . $splitTime ['year'];
$postTime['time'] = $splitTime ['hour'] . ":" . $splitTime ['minute'];

switch ($request)
{
	case 'date':
	return $postTime['date'];
	break;

	case 'time':
	return $postTime['time'];
	break;

	default:
	return false;
	break;	
}
}
?>

 

This simply converts the DateTime to a presentable date and time, so you never actually have to alter the timestamp

It wont let me edit posts so:

 

This is what I tried:

 

$var = format_date($lastlogin) or die("Error");

 

 

Error:

 

Warning: Missing argument 2 for format_date(), called in C:\Server\XAMPP\htdocs\info2011\home.php on line 68 and defined in C:\Server\XAMPP\htdocs\info2011\home.php on line 47

Error

This echo's the values but unformatted. Should the function be able to echo it all in once formatted?

 

and how could I print 5 instead of 05 for the day?

and how could I get it to print the month in words instead of numbers?

 

$date = split_date($lastlogin);

echo $date['day'].' '.$date['month'].' '.$date['year'].' '.$date['hour'].' '.$date['minute'];

 

= 12 05 2008 01 43

Uhh, why not just do this:

 

$logincheck = "SELECT *, DATE_FORMAT("%m-%d-%Y %h:%i:%S", lastlogin) AS lastlog FROM guest WHERE username = '$activeusername' AND password = '$activepassword'";

//echo $query;

$result = mysql_query($logincheck);

 

while($r=mysql_fetch_array($result))

{

$admin=$r["admin"];

$lastlogin=$r["lastlog"];

$name=$r["name"];

}

$logincheck = "SELECT *, DATE_FORMAT(lastlogin, '%M-%D-%Y %l:%i:%S') AS lastlog FROM guest WHERE username = '$activeusername' AND password = '$activepassword'";

 

 

=/

Then do

$lastlogin = $r['lastlog'];

 

And then just echo $lastlogin. It'll be formatted.

 

EDIT: Changed format to your specification.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.