2 variables, random numbers and timestamps, read/write files

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
karossii
Forum Newbie
Posts: 5
Joined: Sat May 30, 2009 6:11 am

2 variables, random numbers and timestamps, read/write files

Post by karossii »

I would like to implement a small php file to do the following when it is called...
  • read an XML file or text file and pull in two variables.
    • the first variable (VAR1) is a number
    • the second variable (VAR2) is a time-stamp
  • compare the current time-stamp to VAR2
    • if the times are more than 1 day apart, add a random number between 180 and 960 for each day apart to VAR1
    • if the times are between 1 hour and 1 day apart, add a random number between 6 and 36 for each hour apart to VAR1
    • if the times are between 1 minute and 1 hour, add a random number between 3 and 12 to VAR1
    • if the times are less than 1 minute apart, add a random number between 1and 5 to VAR1
  • update VAR2 with the current time-stamp
  • In case it isn't obvious, write the updated VAR1 and VAR2 back to the file and close it
This will be integrated into a flash movie I am making... I could use a SQL database instead of a flatfile, but I would rather not.

Thanks for any help you can provide!!!
Last edited by karossii on Mon Jun 01, 2009 12:45 am, edited 1 time in total.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: this should be simple, I think...?

Post by jayshields »

What's the problem?

Just look at rand() and the date functions in PHP.

Also, change your thread title to something more descriptive of your thread content.
top10ingoogle
Forum Newbie
Posts: 3
Joined: Sat May 30, 2009 8:12 am

Re: this should be simple, I think...?

Post by top10ingoogle »

<?php

$now = date('h:i:s A');

if(($now - $var2) < 24hrs) {
$rand = rand(180, 960);
$var2 = $var2 + $rand;
}

// and so on....
?>

non working code but something like this should work of course you'd still have to format the date for seconds or minutes and execute the remaing functions based on var output.
karossii
Forum Newbie
Posts: 5
Joined: Sat May 30, 2009 6:11 am

Re: this should be simple, I think...?

Post by karossii »

jayshields wrote:What's the problem?

Just look at rand() and the date functions in PHP.

Also, change your thread title to something more descriptive of your thread content.
Done and done.

I need three basic pieces of help. Very basic.
1) How do I read/write the file (and how should it be formatted)?
2) How do I compare the timestamps?
3) I am fairly certain this one I can handle, but just in case... how do I set the random number functions?

Thanks!
Post Reply