Solved

PHP Function that makes an array of dates using startdate and enddate

Posted on 2012-03-22
5
402 Views
Last Modified: 2012-03-22
Hi there I need a function that would make an array of dates using the startdate and enddate:

Example

startdate = 03-21-2012
enddate = 03-25-2012

result =
03-21-2012
03-22-2012
03-23-2012
03-24-2012
03-25-2012

Thanks,
0
Comment
Question by:mropenmind
  • 3
  • 2
5 Comments
 

Author Comment

by:mropenmind
ID: 37755043
Thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37755050
0
 

Author Comment

by:mropenmind
ID: 37755060
Good that you've posted that, I forgot that timezone should be EDT or EST (Now it's EDT) , not the one on the server.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37755074
See http://www.laprbass.com/RAY_temp_mropenmind.php

You want your "internal" representations of dates to use the ISO-8601 format.  When you format "pretty" dates for client display you can use strtotime() and date() to format things that are more socially appropriate than the computer code.
<?php // RAY_temp_mropenmind.php
error_reporting(E_ALL);
echo "<pre>";

// REQUIRED SINCE PHP 5.1+
date_default_timezone_set('America/New_York');

// FUNCTION TO RETURN AN ARRAY OF DATES
function array_of_dates($alpha='Today', $omega='Today')
{
    // MIGHT WANT TO ADD SOME SANITY CHECKS HERE
    $out = array();
    $alpha = date('Y-m-d', strtotime($alpha));
    $omega = date('Y-m-d', strtotime($omega));
    while($alpha <= $omega)
    {
        $out[] = $alpha;
        $alpha = date('Y-m-d', strtotime($alpha . ' + 1 DAY'));
    }
    return $out;
}

// TEST THE FUNCTION
print_r( array_of_dates() );

echo PHP_EOL;
print_r( array_of_dates('Today', 'Tomorrow') );

echo PHP_EOL;
print_r( array_of_dates('Yesterday', 'Tomorrow') );

echo PHP_EOL;
print_r( array_of_dates('March 21', 'March 25') );

Open in new window

HTH, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37755415
Thanks for the points.  Just a note in case you missed it in the article.  MySQL and PHP timezones have to be set independently.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now