subtracting time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pauly22
Forum Newbie
Posts: 2
Joined: Tue Feb 22, 2011 8:42 am

subtracting time

Post by Pauly22 »

Hi,

I have one page that writes the time to a text file when called. I have a second page that reads this time and compares it to the current time. I need to subtract the two and have tried using strotime, but it does not work correctly. What am I doing wrong?

Code: Select all

<?php
$myFile = "dates.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 24);
fclose($fh);
echo "$theData <br/><hr>";
$todays_date = date("g:i:s:a");
$today = strtotime($todays_date); 
$exp_date = strtotime($theData);
echo "System status is ";
if ($today - $expiration_date > 30000) { $valid = "Up"; } else { $valid = "Down"; } 
echo($valid);
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: subtracting time

Post by AbraCadaver »

What does $theData look like? If it is a unix timestamp then just subtract it from time(), don't use date(). Also, you use $exp_date and then later compare $expiration_date.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Pauly22
Forum Newbie
Posts: 2
Joined: Tue Feb 22, 2011 8:42 am

Re: subtracting time

Post by Pauly22 »

Thanks, that's helpful.
I'll correct $expiration_date to $exp_date and change date to time.
I don't think the date that is already saved in the text file is a unix stamp, just a date, which is probably my main problem.
Jsphlane20
Forum Newbie
Posts: 17
Joined: Wed Aug 11, 2010 1:17 pm
Contact:

Re: subtracting time

Post by Jsphlane20 »

I agree with AbraCadaver. If you subtract using unix time stamps it will make thins much more simplistic. You could then format the results however you want.
Post Reply