Forum Moderators: coopster

Message Too Old, No Replies

Convert NOW() into PHP timestamp

To check if three months since last logged in

         

s9901470

10:15 am on Jul 29, 2005 (gmt 0)

10+ Year Member



Hi

I have a quiz which people return to three months later. I want to check that they have waited at least three months.

The date they first arrived is stored in a variable 'stamp' using the mysql NOW() command, which enters the data like this;
2005-07-29 09:41:30

I would like to write a script that checks their username against the timestamp and if it is more than three months, allows them to continue.

Perhaps I could convert the NOW() into seconds?

Any suggestions most welcome.

dmorison

10:29 am on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



strtotime() [uk2.php.net] is what you need. Something like:

if (strtotime("2005-02-29 09:41:30") > strtotime("-3 months"))
{
echo("Sorry, you need to wait a bit longer!");
}

Angelis

10:32 am on Jul 29, 2005 (gmt 0)

10+ Year Member



There is also a mysql function to grab a timestamp out of a database.

MYSQL Page: [dev.mysql.com...]

Can go through it with you if you need help, I got confused for about 2 hours when I first looked at it.

s9901470

10:36 am on Jul 29, 2005 (gmt 0)

10+ Year Member



I would prefer to work in seconds because I will be analysing the time taken for each page in seconds.
Is there a way to use mysql to insert "Number of seconds since October 14, 1582" as a variable?

dmorison

10:45 am on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



s9901470,

If you're just interested in seconds rather than actual dates I would recommend working exclusively in time() [php.net] values, stored in your database using a long integer - INT(11).

PHP time() values are seconds since the Unix epoch of January 1 1970 00:00:00 so you'd have to do something more funky if you want to go way back...

s9901470

11:04 am on Jul 29, 2005 (gmt 0)

10+ Year Member



Is there a difference between strtotime("now") and time("now")?

s9901470

11:52 am on Jul 29, 2005 (gmt 0)

10+ Year Member



Got it!

$difference = strtotime("now")-$regdate;
if ($difference > 7776000){echo 'More than 90 days have elapsed so you can now complete part two';}
else {echo '90 days have not elapsed, so you cannot complete part two.';}