[PHP] Timestamp

Status
Not open for further replies.

JayC

Always Learning
Aug 8, 2013
5,497
1,398
I have been trying this for hours. I am not very good with timestamps/dates and I really need a refresher. I tried looking at tutorials before posting and tried so hard to figure it out myself but I need a little help :) Thanks Guys. Heres what I got:

When the button is clicked it updates the users information:
$newTimeStamp = time(); <-- Gets the current time ON THE SERVER
mysql_query("UPDATE user_lottery SET Rolled = '". $newTimeStamp ."' WHERE user_id = '". $_SESSION['user']['id'] ."'");
Then it updates the users entry with that time (Which this part works fine)

So after it gets the time I want to test if its been 24 hours PAST that timestamp.

$checkdatabase = mysql_query("SELECT Rolled FROM user_lottery WHERE user_id = '". $_SESSION['user']['id'] ."'");
So this line above, grabs the Rolled column from user_lottery
if(strtotime($checkdatabase) > strtotime("-24 hours"))
Then this line says if that time is greater then that - 24 hours then do this...
{

But when I update my database manually, and refresh the page EVEN if I make the date smaller, bigger or the same , my code of the button does not show. So here is the whole code:
<?php
$checkdatabase = mysql_query("SELECT Rolled FROM user_lottery WHERE user_id = '". $_SESSION['user']['id'] ."'");
if(strtotime($checkdatabase) > strtotime("-24 hours"))
{
?>
<form method="post">
<input type="submit" value="Free Roll" name="getValues" class="submit"/>
</form>
<?php
}else{
?>
You have already rolled your free roll
<?php
}
?>
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,926
3,923
I'm assuming the question is how to check if it has been 24 hours or not.
PHP:
if($timestampFromDatabase < time() - (60 * 60 * 24))
{
    // It has been 24 hours.
}
else
{
    // It has not been 24 hours.
}
 

JayC

Always Learning
Aug 8, 2013
5,497
1,398
Alright lets see if this works. I was trying to do dates, and I got it but then realized with dates you have to wait 2 days LMAO.
 
I'm assuming the question is how to check if it has been 24 hours or not.
PHP:
if($timestampFromDatabase < time() - (60 * 60 * 24))
{
    // It has been 24 hours.
}
else
{
    // It has not been 24 hours.
}
Well this didn't work but it helped me to the solution
$getRolled = mysql_query("SELECT * FROM user_lottery WHERE user_id='". $_SESSION['user']['id'] ."'");
while($Rolled = mysql_fetch_array($getRolled)){
if($Rolled['Rolled'] < date('Y-m-d H:m:s',strtotime('last day')))
I used a while loop (So it would ALWAYS grab new data, even if I updated it manually) and then checked the date like that and it works :)
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,396
960
I'm assuming the question is how to check if it has been 24 hours or not.
PHP:
if($timestampFromDatabase < time() - (60 * 60 * 24))
{
    // It has been 24 hours.
}
else
{
    // It has not been 24 hours.
}
You should first check difference between current time and the timestamp form the database. Then if difference is greater than 60*60*24, it's been 24 hours.
 

JayC

Always Learning
Aug 8, 2013
5,497
1,398
Already solved it, but thanks guys
Alright lets see if this works. I was trying to do dates, and I got it but then realized with dates you have to wait 2 days LMAO.
 

Well this didn't work but it helped me to the solution
$getRolled = mysql_query("SELECT * FROM user_lottery WHERE user_id='". $_SESSION['user']['id'] ."'");
while($Rolled = mysql_fetch_array($getRolled)){
if($Rolled['Rolled'] < date('Y-m-d H:m:s',strtotime('last day')))
I used a while loop (So it would ALWAYS grab new data, even if I updated it manually) and then checked the date like that and it works :)
 
Status
Not open for further replies.

Users who are viewing this thread

Top