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

user2521365
  • 131
  • 3
  • 17

3 Answers3

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;
rack_nilesh
  • 553
  • 5
  • 18
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";
}
user3821538
  • 117
  • 4
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`
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63