Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date array

Status
Not open for further replies.

rbauerle

Programmer
Oct 16, 2001
48
BR
I need to build an array containnig all dates between two other dates (date1 and date2, witch are variables). My problem is: i don´t know very well how to manipulate dates.
that would have to be:
$date1 = 2004-07-30
$date2 = 2004-08-03

$array[0] = "2004-07-30"
$array[1] = "2004-07-31"
$array[2] = "2004-08-01"
$array[3] = "2004-08-02"
$array[4] = "2004-08-03"

Any help is very welcome.
Thanks.
 
Here's one way of doing this. Probably not the most efficient, but it does answer your question:
Code:
<?
        $test = strtotime("2004-07-30");
        $end = strtotime("2004-08-03");
        while ($test <= $end) {
                $date_array[] = date("Y-m-d",$test);
                $test = strtotime(date("Y-m-d",$test) . "+1 day"); }
        echo '<pre>';
        print_r($date_array);
        echo '</pre>';
?>
Ken
 
I'd stick with kenrbnsn's logic, but I wouldn't keep going back and forth with strtotime because that introduces the inneffiency.

Code:
<?php
$test=strtotime($start_date);
$end =strtotime($end_date);

while ($test < $end) {
  $date_array[]=date('Y-m-d',$test);  //or whatever format you like
  $test += 86400;
}
?>
 
But I think the 'postee' wants to include the last date (end) so using skiflyer's example:
Code:
<?php
$test=strtotime($start_date);
$end =strtotime($end_date);

while ($test <= $end) {
  $date_array[]=date('Y-m-d',$test);  //or whatever format you like
  $test += 86400;
}
?>

just a little explanation:
strtotime converts the date string to time in seconds
86400 represents one day in seconds.

[cheers]
Cheers!
Laura
 
Good catch LTeeple.

Side note, we're assuming $start_date & $end_date are parsable by strtotime... if not, you'll need to do that step in more complicated manners using the date or time functions.

Additionally, to be safe I'd probably get anal and do something like
Code:
$test=strtotime(date("Y-m-d 00:00:01"), strtotime($start_date));
$end =strtotime(date("Y-m-d 11:59:59"), strtotime($end_date));

It may be overkill, but if your start & end dates include time of day, and the end date had an early time of day than the start date, you wouldn't successfuly include the end date in the array without it.

If you actually have the Year Month and Date seperated... you can have a "better" solution with
Code:
$test=mktime(0,0,0,$start_month,$start_day,$start_year);
$end=mktime(0,0,0,$end_month,$end_day,$end_year);
or
$end=mktime(11,59,59,$end_month,$end_day,$end_year);

This will get you past any of the strtotime() quirks if you can't guarantee you input.
 
Excellent point skiflyer.

As well, the php function checkdate() can validate a gregorian date.
so:
Code:
list($year,$month,$day) = explode("-",$date);
settype($year,"integer");
settype($month,"integer");
settype($day,"integer");
$datecheck = checkdate($month,$day,$year);
the function checkdate will return a bool true or false.

[cheers]
Cheers!
Laura
 
thank you all!! that was more then helpfull.

Renata
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top