473,385 Members | 2,028 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,385 software developers and data experts.

generating a list of dates

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
11 2464
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
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
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
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


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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: GIMME | last post by:
Background ... I've created a web application that allows a user to create an HTML application from IE. The application itself creates an XML representation of a XHTML form. The XHTML...
5
by: Henry | last post by:
Dear all, Is there any DB2 SQL statement that could generate rows of intermediate dates by providing the start and end dates?? If 2006-1-1 and 2006-1-31 are provided to the SQL, the output...
1
by: pitfour.ferguson | last post by:
My dbase has the start date and end date of each visit. How can I ask Access to list the day of the week of the start (easy), end (easy) and, more importantly, the dates of the visit itself - ie...
1
by: sreedivya | last post by:
Hi, I am using Crystal Reports with .NET 2.0 for generating reports in my project. My database is SQLSERVER 2000. I want to display list of absentees between 2 given dates. For this I have written...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.