Johnny2 Posted February 28, 2013 Report Share Posted February 28, 2013 I'm trying to calculate the difference between timestamps. Can't figure out why it's not working. The particular line of code that isn't giving me what I expect is: $difference = abs(($timestamp)-($birthday)); I'm expecting a result of the number of seconds between the two timestamps. Below is all of the webpage code: <?php //creates a timestamp for right now $timestamp=strtotime("now"); //formats a timestamp into a human-readable form $date = date("Y m d H:i:s",$timestamp); //defines Birthday $birthday = "August 14, 1975"; //takes a date and converts it to a timestamp. strtotime(timeStringToParse,timestamp) ommit timestamp to use current time (whatever that means) $convertedtotimestamp = strtotime($birthday); //calculates the difference between two timestamps (in seconds) $difference = abs(($timestamp)-($birthday)); //difference converted into human-readable format (years in this case) $diffReadable = ($difference/60/60/24/365); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Working with timestamps</title> </head> <body> <?php echo "current timestamp:"; echo "<br />"; echo $timestamp; echo "<br />"; echo "<br />"; echo "today's date:"; echo "<br />"; echo $date; echo "<br />"; echo "<br />"; echo "Birthday:"; echo "<br />"; echo $birthday; echo "<br />"; echo "<br />"; echo "Birthday's timestamp:"; echo "<br />"; echo $convertedtotimestamp; echo "<br />"; echo "<br />"; echo "the difference between today and birthday (in seconds)"; echo "<br />"; echo $difference; echo "<br />"; echo "<br />"; echo "Age: "; echo "<br />"; echo $diffReadable; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted February 28, 2013 Report Share Posted February 28, 2013 The problem is on line 15 -- you are subtracting the current timestamp minus the readable date. Instead, you need to be doing: $difference = abs(($timestamp)-($convertedtotimestamp)); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.