-1

I am trying to save date when the user log out so that he can find the date of the last login, I am using this code:

 mysql_query("UPDATE `table` SET `lastLog` = now() WHERE user_id = '".$_SESSION['userID']."'"); 

which will give the history and the time of the last login correctly but I want the code to show the word today if the last login was today and yesterday if the last login was yesterday. and after yesterday I want to show only the regular date for example if I logged out three days ago then the value should be like this: 8/7/2014. is there an easy way to do it.

4
  • 3
    Do it in PHP, there is plenty of ways, have a search, also, I hope you dont allow userids/usernames like 1' or '1'='1 Commented Jul 11, 2014 at 7:39
  • So, if you store today in your database, what happens to that value 3 days later? Commented Jul 11, 2014 at 7:43
  • Check out, php date yesterday today Commented Jul 11, 2014 at 7:44
  • @John Bupit its supposed to show the regular date for example if I logged out three days ago then the value should be like this: 8/7/2014 Commented Jul 11, 2014 at 7:47

3 Answers 3

3
 $result = mysql_query("select lastLog from `table` WHERE user_id = '".$_SESSION['userID']."'"); while($row = mysql_fetch_array($result)) { $last_login = $row['lastLog ']; } if($last_login == date('d.m.Y')) echo "its today"; if($last_login == date('d.m.Y',strtotime("-1 days"))) echo "it was yesterday"; else echo 'it was '.$last_login; 
Sign up to request clarification or add additional context in comments.

Comments

2

Firstly, you are going to want to be using MYSQLI rather than mysql. Mysql is outdated and deprecated and MYSQLI is the newer, more stable child of mysql php syntax.

Second, this should point you in the right direction of how to achieve what you need.

$today = date('FORMAT OF DATE FROM QUERY'); $yesterday = date('FORMAT OF DATE FROM QUERY', strtotime("yesterday")); if ($result === $today) { $result = "today"; } elseif ($result === $yesterday) { $result = "yesterday"; } 

Comments

1

If you want to select the data and display 'Today/Yesterday' or 'Date' depending on the value you can do so using case-when in the query something as

select col1, col2, ..... case when date(lastLog) = curdate() then 'Today' when date(lastLog) = curdate()-INTERVAL 1 day then 'Yesterday' else lastLog end as lastLog from `table` 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.