Solved

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

Posted on 2012-03-22
5
409 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

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.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

623 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