Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article discusses four methods for overlaying images in a container on a web page
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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.

757 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

22 Experts available now in Live!

Get 1:1 Help Now