By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,746 Members | 1,735 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,746 IT Pros & Developers. It's quick & easy.

generating a list of dates

P: n/a
Hi,

Using PHP 4.4.4, I want to generate an array that contains strings
representing days from Jan. 1, 2005 to whatever the current day is.
So the array would contain the first values

'2005-01-01', '2005-01-02', '2005-01-03'

and end with the values

'2007-01-27', '2007-01-28'

In case you're interested, I will then take this array and populate a
MySQL table.

Thanks for any advice, - Dave

Jan 29 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Should do it. Uses unix time stamps.

<?php
function generate_dates($start, $end)
{
$start = strtotime($start);
$end = strtotime($end);

$oneday = 86400;

$return = array();

for ($current = $start; $current <= $end; $current+=$oneday)
{
$return[] = $current;
}

return $return;
}

print '<pre>'; print_r(generate_dates('jan 1, 2005','today'));
?>
Hi,

Using PHP 4.4.4, I want to generate an array that contains strings
representing days from Jan. 1, 2005 to whatever the current day is.
So the array would contain the first values

'2005-01-01', '2005-01-02', '2005-01-03'

and end with the values

'2007-01-27', '2007-01-28'

In case you're interested, I will then take this array and populate a
MySQL table.

Thanks for any advice, - Dave
--
Carl Vondrick
www.CarlSoft.net
Jan 29 '07 #2

P: n/a
la***********@zipmail.com wrote:
Using PHP 4.4.4, I want to generate an array that contains strings
representing days from Jan. 1, 2005 to whatever the current day is.
So the array would contain the first values
<?php
$D = array(); // For results
$f = 'Y-m-d'; // Desired date format
$d = 1104537600; // 1 Jan 2005, Unix timestamp
while ($d < time()) // While still in the past...
{
$D[] = date($f,$d); // Add this date to the list
$d += 24*60*60; // The next date is 24 hours later
}
print_r($D); // Let's see what we've got!
?>

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Jan 29 '07 #3

P: n/a
Carl Vondrick wrote:
$oneday = 86400;
Watch out for daylight-saving time. There may be Days longer or shorter
than 24h! Never start at Midnight.

Or use mktime. mktime(0,0,0,2,0,2007) is Jan. 31. 2007 and
mktime(0,0,0,1,32,2007) is Feb. 1.

Heiko
--
http://portal.richler.de/ Namensportal zu Richler
http://www.richler.de/ Heiko Richler: Computer - Know How!
http://www.richler.info/ private Homepage
Jan 29 '07 #4

P: n/a
Heiko Richler wrote:
Watch out for daylight-saving time. There may be Days longer or shorter
than 24h! Never start at Midnight.
That's a good point. You can workaround it by using UTC though, as UTC
doesn't have daylight savings.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Jan 29 '07 #5

P: n/a


On 29 Jan, 07:28, Heiko Richler <heiko-rich...@nefkom.netwrote:
Carl Vondrick wrote:
$oneday = 86400;Watch out for daylight-saving time. There may be Days longer or shorter
than 24h! Never start at Midnight.

Or use mktime. mktime(0,0,0,2,0,2007) is Jan. 31. 2007 and
mktime(0,0,0,1,32,2007) is Feb. 1.

Heiko
--http://portal.richler.de/Namensportal zu Richlerhttp://www.richler.de/ Heiko Richler: Computer - Know How!http://www.richler.info/ private Homepage
Yes, DST is a real gotcha. I had a similar problem which was discussed
in this forum a while ago.

I was advised to use strtotime. My query was regarding adding numbers
of weeks. In your case you'd use something like:

$adate = strtotime("+1 day", strtotime($adate));

Jan 29 '07 #6

P: n/a
Heiko Richler wrote:
Watch out for daylight-saving time. There may be Days longer or shorter
than 24h! Never start at Midnight.
Good point!

KRAMER: Look how dark it's gettin' already.
JERRY: Well, it's not Daylight Savings Time yet.
KRAMER: When does it start?
JERRY: I don't know, they just tell you the night before.
KRAMER: Uh. Well, I'm sick o' waiting. I am springin' ahead riiight now.
JERRY: Oh, I'm sure that won't cause any problems..

--
Carl Vondrick
www.CarlSoft.net
Jan 30 '07 #7

P: n/a
On Jan 28, 9:39 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Hi,

Using PHP 4.4.4, I want to generate an array that contains strings
representing days from Jan. 1, 2005 to whatever the current day is.
So the array would contain the first values

'2005-01-01', '2005-01-02', '2005-01-03'

and end with the values

'2007-01-27', '2007-01-28'

In case you're interested, I will then take this array and populate a
MySQL table.

Thanks for any advice, - Dave
I thought I'd see if I could figure it out my own version. This seems
to work:

function daysUntilNow($month, $day, $year, $format = 'Y-m-d') {
$d = array();
$ts = 0;

while ( ($ts = mktime(0,0,0,$month,$day,$year)) < time() ){
$ts = mktime(0,0,0,$month,$day,$year);
$d[] = date($format,$ts);
$day++; // increment days
}
return $d;
}

Jan 30 '07 #8

P: n/a
On Jan 29, 6:46 pm, "Curtis" <dye...@gmail.comwrote:
function daysUntilNow($month, $day, $year, $format = 'Y-m-d') {
$d = array();
$ts = 0;

while ( ($ts = mktime(0,0,0,$month,$day,$year)) < time() ){
$ts = mktime(0,0,0,$month,$day,$year);
$d[] = date($format,$ts);
$day++; // increment days
}
return $d;

}
Grr, Google Groups borked my code. Pretend there's a closing brace for
the function.

Jan 30 '07 #9

P: n/a
Rik
Curtis <dy****@gmail.comwrote:
On Jan 29, 6:46 pm, "Curtis" <dye...@gmail.comwrote:
>function daysUntilNow($month, $day, $year, $format = 'Y-m-d') {
$d = array();
$ts = 0;

while ( ($ts = mktime(0,0,0,$month,$day,$year)) < time() ){
$ts = mktime(0,0,0,$month,$day,$year);
$d[] = date($format,$ts);
$day++; // increment days
}
return $d;

}

Grr, Google Groups borked my code. Pretend there's a closing brace for
the function.
There is in real newsreaders :P

--
Rik Wasmus
Jan 30 '07 #10

P: n/a
Rik wrote:
Curtis <dy****@gmail.comwrote:
>On Jan 29, 6:46 pm, "Curtis" <dye...@gmail.comwrote:
>>function daysUntilNow($month, $day, $year, $format = 'Y-m-d') {
$d = array();
$ts = 0;

while ( ($ts = mktime(0,0,0,$month,$day,$year)) < time() ){
$ts = mktime(0,0,0,$month,$day,$year);
$d[] = date($format,$ts);
$day++; // increment days
}
return $d;

}

Grr, Google Groups borked my code. Pretend there's a closing brace for
the function.

There is in real newsreaders :P

--Rik Wasmus
Yeah, I finally found out my ISP's news server. I'm using Thunderbird
as my news client, for the moment, but I still want to check out Opera.

I hope Thunderbird is breaking the lines after the right amount of
characters.

--
Curtis, free of Google Groups
Jan 30 '07 #11

P: n/a
Curtis wrote:
I hope Thunderbird is breaking the lines after the right amount of
characters.
Yep, and it seems to be using format=flowed too, which can be a bonus for
some people.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 1 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.