close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Timestamp Calculations

Thread began 9/07/2009 7:52 am by Kevin373660 | Last modified 9/10/2009 12:29 pm by Kevin373660 | 5429 views | 7 replies |

Kevin373660

Timestamp Calculations

I am using DataAssist and eCart to handle a user pass renewal system with php and MySQL. I am stuck with calculating dates and need some direction from a fellow member of the forum. I have been echoing the values on screen simply for testing purposes so that I can verify my math before doing the record update. My understanding is that using TimeStamp the dates are stored in seconds.

I need to be able to:

1) Calculate the new Expiry Date (in a format ready for record update to db schema table) NewActivationDate=TodaysDate+(NewPassLength+RemainingPassLength)
2) Calculate the days remaining in a Pass (display as whole number on screen for user)

After doing the calculations, I know how to do the update using DataAssist record update server behavior. That part is easy, allows additional control with session variables that the standard update record behavior does not have and it works.

With this code:
<?php
echo $row_WAATKtblmembers['passActivation'];
echo $row_WAATKtblmembers['passExpireDate'];
echo strtotime($row_WAATKtblmembers['passActivation']), "\n";
echo strtotime($row_WAATKtblmembers['passExpireDate']), "\n";
echo $_SESSION['ActivationPassLength'];
?>

Outputs to screen the following:
2009-09-01 00:00:00
2009-09-10 00:00:00
1251777600
1252555200
6

However, a simple math calc:
<?php
$datetoday = $_SERVER['REQUEST_TIME'];
$passExpireDate = $row_WAATKtblmembers['passExpireDate'];
$passDaysRemaining = ($passExpireDate - $datetoday);
echo $passDaysRemaining;
?>

I expected a positive value like 777600 however, it Outputs to screen the following negative value: -1252332020

Sign in to reply to this post

DarylBeta Tester

Try having a look at a question that I asked about calculating a persons age from date of birth as it might help.... (it might not)

http://www.webassist.com/forums/showthread.php?t=1886

Don't look at my solution, but try the one at the bottom of the thread by the WA gurus.

Sign in to reply to this post

Kevin373660

Thank you Daryl for replying to my post. It has part of the puzzle. I miss the days when everyone in the programming community help each other. I first searched the forum for clues and had found your thread.

Perhaps is it just the stress that I am under, but I have not found a clear and accurate document or book that puts everything together. I have looked at php.net and mysql.com but I still don’t clearly see the whole picture. My application involves using the timestamp values from the db schema in mathematical formulas, updating mysql timestamp columns with results and echoing some of the results for the users. From what I have read this involves seconds, integers and converting data formats. On the web there seems to be a lot of inaccurate examples and disinformation. Hopefully, others in the forum that have run into this or the WA gurus, which have been so helpful before, can help clarify this for me and for other members.

There is another useful Webassist link php-scripts/ with a code script to Format Dates.

Functions used:
datediff
strtotime
abs
floor

***** Current Test Code Results ****
<?php
echo strtotime($row_WAATKtblmembers['passActivation']), "\n";
echo strtotime($row_WAATKtblmembers['passExpireDate']), "\n";
?><br />
MATH FUNCTION TEST AREA DAYS LEFT 2:<br />
<?php
$passExpireDate = $row_WAATKtblmembers['passExpireDate'];
$passActivationDate = $row_WAATKtblmembers['passActivationDate'];
$seconds = abs(strtotime($passExpireDate) - strtotime($passActivationDate));
$passDaysLeft = floor($seconds);
echo $passDaysLeft;
?>

Screen Display:
TEST CODE:
1251777600 1252555200
1252419760
MATH FUNCTION TEST AREA DAYS LEFT 2:
1252555200

Sign in to reply to this post

Justin Nemeth

Are you storing dates in MySQL using their timestamp column type? If so, that might be what is causing some confusion. A MySQL timestamp is not the same as a php timestamp. The conventional timestamp (aka unix timestamp) is a number of seconds. MySQL stores something else which is harder to work with in php.

So, assuming you are indeed using the MySQL timestamp, I would use the MySQL UNIX_TIMSTAMP() function to have your query return a php timestamp. Then using the date() and strtotime() functions with the php timestamp should get you going on the right track. strtotime() is one of the most versatile functions for manipulating dates.

-justin

Sign in to reply to this post

Kevin373660

Thank you Justin for your comments. Yes, I am using MySQL with the column datatype set to timestamp. So the #1 step is to convert the column info into a workable format, #2 do the math, #3 then convert result back into the correct format to update the table.

I have used DataAssist and SecurityAssist to query, authenticate and update the tables with multiple (6 to 27) columns with only 2 or 3 columns using the timestamp datatype.

$row_WAATKtblMySql['passExpireDate'];

How would I use the MySQL UNIX_TIMSTAMP() function to have the query return a php timestamp? Or should I just change the format within the code?

I found this example but I can&#8217;t get it to work.

<?PHP
// converts $unix_timestamp to "normal" formated_datetime:
$formated_datetime = DATE("Y-m-d H:i:s",$unix_timestamp);

// produces a timestamp that looks like "YYYY-MM-DD 24:00:00":
$formated_datetime = DATE("Y-m-d H:i:s");

// takes $formated_datetime and converts to "UNIX timestamp":
$unix_timestamp = STRTOTIME($formated_datetime);
?>

Sign in to reply to this post

Kevin373660

I figured out how to bring the unix_timestamp format in to the php format as code below. This was complicated because one of the passActivationDate column which also uses the timestamp datatype did not seem to need conversion from the Unix format. Does anyone know why this may have occured?

Is is related to the method that the data is entered? Through an update or directly on the table through my GUI using Navicat.

I am working on the math calc's. To update the table schema I am assuming that I need to convert the values? Then run the DataAssist update with those properly formated values. Is this the correct way to go from php timestamp to mysql timestamp?

<?PHP
// takes $formated_datetime and converts to "UNIX timestamp":
$newExpiryDate_MySQL = strtotime("$newExpiryDate");
?>

Sign in to reply to this post

Justin Nemeth

In your SQL query, you could also do something like this:

SELECT UNIX_TIMESTAMP(passExpireDate) AS pass_timestamp ....

That would convert the MySQL timestamp column into a more php friendly unix timestamp.

Sign in to reply to this post

Kevin373660

For the benifit of other forum members, this is what I found to work. You can use the $newExpiryDate_MySQL in your MySQL record update/insert.

<?php
//CONVERT NEW PASS EXPIRY DATE $newExpiryDate_MySQL
$newExpiryDate_MySQL = date("Y-m-d H:i:s", $newExpiryDate);
//echo $newExpiryDate_MySQL
?>

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...