Solved

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

Posted on 2012-03-22
5
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:mropenmind
ID: 37755043
Thanks
0
 
LVL 110

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 110

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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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