473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to increment array elements with loop?

I need to populate a file with past unix time stamps over a period of time
specified by the user. The time stamps should be evenly spaced, based on
user-defined parameters.

My question is: How do I add an element to an array, then increment it and
loop so that each element builds on the previous element just added?

Here's a first crack at it:

<?php
$data_file = 'timedata.txt';
$avdaily = 25 //user-defined
$total_count = (183 * $avdaily) //time period = six months
$interval = (3600*24)/$avdaily //evenly spaced intervals
$first_time = time() - (3600*24*183)
$next_time = ($first_time + $interval)
//add $next_time to $data_array -- here's where I'm lost
$next_time = ($next_time + $interval)
//add $next_time to $data_array
//loop until $data_array has $total_count elements
//write $data_array to $data_file
//newest time should be last line in file
?>

This output should be a file that has a
series of unix time stamps incremented
by the amount of $interval. The below output
example increments by 10 seconds each iteration.

1081111110
1081111120
1081111130
1081111140
1081111150
1081111160
1081111170
1081111180
1081111190

Any help is appreciated! Thanks.
Jul 17 '05 #1
2 16707
deko wrote:
I need to populate a file with past unix time stamps over a period of time
specified by the user. The time stamps should be evenly spaced, based on
user-defined parameters.

My question is: How do I add an element to an array, then increment it
and loop so that each element builds on the previous element just added?

Here's a first crack at it:

<?php
$data_file = 'timedata.txt';
$avdaily = 25 //user-defined
$total_count = (183 * $avdaily) //time period = six months
$interval = (3600*24)/$avdaily //evenly spaced intervals
$first_time = time() - (3600*24*183)
$next_time = ($first_time + $interval)
//add $next_time to $data_array -- here's where I'm lost
$next_time = ($next_time + $interval)
//add $next_time to $data_array
//loop until $data_array has $total_count elements
//write $data_array to $data_file
//newest time should be last line in file
?>

This output should be a file that has a
series of unix time stamps incremented
by the amount of $interval. The below output
example increments by 10 seconds each iteration.

1081111110
1081111120
1081111130
1081111140
1081111150
1081111160
1081111170
1081111180
1081111190

Any help is appreciated! Thanks.


How about something like this?

function build_timestamp_array($start_timestamp, $interval, $number) {

$array = array();
for($i = 0; $i < $number; $i++) {
$array[] = $start_timestamp + $interval * $i;
}
return $array;

}
--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2
> How about something like this?

function build_timestamp_array($start_timestamp, $interval, $number) {

$array = array();
for($i = 0; $i < $number; $i++) {
$array[] = $start_timestamp + $interval * $i;
}
return $array;

}


I'm somewhat of a rookie with functions - this is what I think it would look
like in pseudo code:

<?php
$data_file = 'timedata.txt';
$avdaily = 25 //user-defined
$total_count = (183 * $avdaily) //time period = six months
$interval = (3600*24)/$avdaily //evenly spaced intervals
$first_time = time() - (3600*24*183)
//$next_time = ($first_time + $interval)

for($i = 0; $i < $total_count; $i++) {
$timestamp_array[] = $first_time + ($interval * $i);
}

???

//newest time should be last line in file
?>

This output should be a file that has a
series of unix time stamps incremented
by the amount of $interval. The below output
example increments by 10 seconds each iteration.

1081111110
1081111120
1081111130
1081111140
1081111150
1081111160
1081111170
1081111180
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: deko | last post by:
I have a counter file that records page hits - each hit is a UNIX timestamp in the file. But I'm only interested in page hits in the last 365 days. The below code creates an array from the file...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
3
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
8
by: ljlevend2 | last post by:
If a property's type is an array and the property is exposed in a PropertyGrid, then the property can be expanded so that the user can view and edit the array elements. But, I've noticed that if...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
6
by: Ciaran | last post by:
Im a php coder - not really used to javascript. Can someone please tell me how to assign labels to array elements and then call them by the index? var mycars = new Array(3) mycars = "Saab"...
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
4
by: Bob | last post by:
Is there a faster way to add the array elements to a list then looping through the elements like: double toadd; list<doublelistwant; for (int i=0;i<toadd.Length;i++){ listwant.Add(toadd);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.