Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-22
5
Medium Priority
?
415 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

783 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