PHP Classes
elePHPant
Icontem

PHP Date Interval: Perform calculations with date intervals

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-11-29 (4 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: 150 All time: 8,612 This week: 415Up
Version License PHP version Categories
date-frame 1.1MIT/X Consortium ...5.3PHP 5, Time and Date, Databases
Description Author

This class can perform calculations with date intervals.

It takes a start and a begin date and performs several types of calculations related with the interval defined by those dates.

Currently it can check if a date is within an interval, check if two intervals overlap, generate SQL BETWEEN clauses to check if an expression is within the date range, get the list of days in the interval separated by days, weeks, months, quarters or years.

  Performance   Level  
Name: David Giglio <contact>
Classes: 1 package by
Country: United States United States

Details

DateFrame

PHP DateFrame class to ease working with date intervals (date ranges), and ease generation of SQL BETWEEN clauses

Contructor requires start and end date in any format readable aby PHP's strtotime function

$dateframe = new DateFrame('2015-01-01', '3/31/2015');
/
 * DateFrame Object
 * (
 *   [start] => 2015-01-01 00:00:00
 *   [end] => 2015-03-31 23:59:59
 *   [days] => 90
 *   [criteria] => 
 *   [orig_start] => 2015-01-01
 *   [orig_end] => 3/31/2015
 * )
 */

Each DateFrame can then be broken into an array of shorter interval DateFrames broken down by: day, seven days, Sun-Sat weeks, n months, fiscal quarters, n years.

$by_month = $dateframe->asMonthIntervals(); 
/
 * $by_month set to array of three DateFrame objects: Jan., Feb., Mar.
 *
 * Array
 * (
 *   [0] => DateFrame Object
 *       (
 *          [start] => 2015-01-01 00:00:00
 *          [end] => 2015-01-31 23:59:59
 *          [days] => 31
 *          [orig_start] => 2015-01-01
 *          [orig_end] => 2015-01-31
 *          [interval] => 1M
 *      )
 *
 *  [1] => DateFrame Object
 *      (
 *          [start] => 2015-02-01 00:00:00
 *          [end] => 2015-02-28 23:59:59
 *          [days] => 28
 *          [orig_start] => 2015-02-01
 *          [orig_end] => 2015-02-28
 *          [interval] => 1M
 *      )
 *
 *   [2] => DateFrame Object
 *      (
 *          [start] => 2015-03-01 00:00:00
 *          [end] => 2015-03-31 23:59:59
 *          [days] => 31
 *          [orig_start] => 2015-03-01
 *          [orig_end] => 2015-03-31
 *          [interval] => 1M
 *      )
 * )
 */

Methods to check if DateFrame contains date, or DateFrame overlaps another DateFrame.

$dateframe->contains('2/1/2015'); // equals true

$dateframe2 = new DateFrame('2014-01-01', '3/31/2014');
$dateframe->overlaps($dateframe2); // equals false

Method to create SQL BETWEEN clause

$dateframe = new DateFrame('2015-01-01', '3/31/2015');
$between = $dateframe->getCriteria('completed_date');
/
 * DateFrame Object
 * (
 *   [start] => 2015-01-01 00:00:00
 *   [end] => 2015-03-31 23:59:59
 *   [days] => 90
 *   [criteria] => completeddate BETWEEN '2015-01-01 00:00:00' AND '2015-03-31 23:59:59'
 *   [orig_start] => 2015-01-01
 *   [orig_end] => 3/31/2015
 * )
 */

  Files folder image Files  
File Role Description
Plain text file DateFrame.php Class Class source
Accessible without login Plain text file DateFrame_Test.php Test Unit test script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:150
This week:0
All time:8,612
This week:415Up