Jump to content

Calculating Difference Between Timestamps


Johnny2

Recommended Posts

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>

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...