Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Convert MySQL timestamp field to a Unix timestamp

Explorer ,
Oct 07, 2012 Oct 07, 2012

I have a process where I want to compare the current date with an expiration date that is so many months after a registration date. The registration date is stored in a MySQL timestamp format. I want to convert this to a Unix timestame so I can do the comparison with Time(). I have tried both the MySQL UNIX_TIMESTAMP and the PHP getTimestamp() functions and cannot get them to work. I have tried UNIX_TIMESTAMP both in  the original SELECT statement and as a stand alone one. I also get a divide-by-zero error with the date_default_timezone_set function. I have enclosed screenshots of the code and the result of the echo at the bottom. THANKS!

Expire Test.jpg

RegDate error.jpg

TOPICS
Server side applications
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 08, 2012 Oct 08, 2012

You're making it far too complicated.

SELECT TO_DAYS(reg_date) - TO_DAYS(CURDATE()) AS daysleft

FROM ssregis

WHERE. . .

That will give you the number of days left until the registration expires. If it's less than 0, redirect to the renewal page.

By the way, you're laying yourself wide open to SQL injection attacks by failing to escape the input from the query string. At the very least, you should be doing this:

$entered_ss_id = mysql_real_escape_string($_GET['record_id']);

$entered_email = mysql_real_e

...
Translate
LEGEND ,
Oct 07, 2012 Oct 07, 2012

I'm confused at what you are doing. Why not just use a MySQL date arithmatic function like DATE_ADD() to add the number of months to the reg date to arrive at the expiration date:

SELECT DATE_ADD (reg_date, INTERVAL ss_nomonths MONTH) as ExpireDate

or use a PHP date math function?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2012 Oct 08, 2012

You're making it far too complicated.

SELECT TO_DAYS(reg_date) - TO_DAYS(CURDATE()) AS daysleft

FROM ssregis

WHERE. . .

That will give you the number of days left until the registration expires. If it's less than 0, redirect to the renewal page.

By the way, you're laying yourself wide open to SQL injection attacks by failing to escape the input from the query string. At the very least, you should be doing this:

$entered_ss_id = mysql_real_escape_string($_GET['record_id']);

$entered_email = mysql_real_escape_string($_GET['email_address']);

However, since it looks as though you're hand-coding the script, you should be using MySQL Improved rather than PHP's deprecated MySQL functions. See http://docs.php.net/manual/en/mysqlinfo.api.choosing.php.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2012 Oct 08, 2012

THANKS a lot to both Bregent and David! I implemented both of  David's suggestions and they worked like a charm. I particularly appreciated the specific implementation into my code.

Can you tell me why

date_default_timezone_set(America/Vancouver);

gives me a divide by zero error?

Thanks again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 09, 2012 Oct 09, 2012

MartyMatthews wrote:

Can you tell me why

date_default_timezone_set(America/Vancouver);

gives me a divide by zero error?

The timezone identifier must be a string:

date_default_timezone_set('America/Vancouver');

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 09, 2012 Oct 09, 2012

Thanks David! I shouldn't have had to ask!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 09, 2012 Oct 09, 2012

No problem. It's easy to miss something like that. The code looked correct, and it wasn't immediately obvious that the quotes were missing.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 09, 2012 Oct 09, 2012
LATEST

I appreciate your willingness to answer such a simple question!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines