|
|
|
Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
mySQLdateBetween() determines if a given date falls within a given time span.
Arguments are either arrays (as used by DateSpan.php) or MySQL date strings.
Acts as MySQL layer to DateSpan.php (http://www.weberdev.com/get_example-4515.html)
Zip archive contains :
MySQLdateSpan.php : The functions code
testMySQLdateSpan.php : A test script
test.sql : An sql script for instantiating the test MySQL database.
DateSpan.php can be found at : http://www.weberdev.com/get_example-4515.html
MySQLdateSpan.php
| <?php
/*
* (c) 2006, D.E. Silvia, All rights reserved.
* This code is available for use for non-commercial purposes.
* Free to distribute as long as this copyright information remains intact.
* No modification is authorized. Please, refer bugs/enhancements to
* dsilvia@mchsi.com
*
*/
/*
*
* Simple conversion functions to change MySQL dates to arrays,
* arrays to MySQL dates.
*
* mySQLdateBetween() determines if a given date falls within a
* given time span. Arguments are either arrays (as used by
* DateSpan.php) or MySQL date strings.
*
* Acts as MySQL layer to DateSpan.php
*
*/
include_once('DateSpan.php');
function mySQLdateBetween($startDate,$endDate,$findDate)
{
if(gettype($startDate) == "string")
$startDate=mySQLdate2Ary($startDate);
if(gettype($endDate) == "string")
$endDate=mySQLdate2Ary($endDate);
if(gettype($findDate) == "string")
$findDate=mySQLdate2Ary($findDate);
return dateBetween($startDate,$endDate,$findDate);
}
function mySQLdate2Ary($mySQLdateStr)
{
return array_reverse(split('-',$mySQLdateStr));
}
function ary2mySQLdate($ary)
{
return implode('-',array_reverse($ary));
}
?> | |
testMySQLdateSpan.php
| <?php
include_once('MySQLdateSpan.php');
$rp=mysql_connect(localhost,root);
$dbSelected=mysql_select_db('test');
$request=mysql_query('select * from `dates`');
$dateRow=mysql_fetch_row($request);
$start=$dateRow[0];
$end=$dateRow[1];
$request=mysql_query('select * from `anniversaries`');
$today=getdate();
$thisYear=$today[year];
while($annivRow=mysql_fetch_row($request))
{
$name=$annivRow[0];
$anniv=mySQLdate2Ary($annivRow[1]);
$which=$thisYear-$anniv[2];
$anniv[2]=$thisYear;
if(mySQLdateBetween($start,$end,$anniv))
{
print($name."'s $which year anniversary is on ".ary2mySQLdate($anniv)." in the period between $start and $end<br />");
}
else
{
print($name."'s anniversary is not in the period between $start and $end<br />");
}
}
$request=mysql_query('select * from holidays');
print("<br />During the period between $start and $end, the Kansas City Board of Trade observes the following holidays<br/>");
while($holidayRow=mysql_fetch_row($request))
{
if(mySQLdateBetween($start,$end,$holidayRow[1]))
{
print($holidayRow[0]." is observed on ".$holidayRow[1]."<br />");
}
}
?> | |
test.sql
| -- phpMyAdmin SQL Dump
-- version 2.8.2.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 12, 2006 at 01:49 PM
-- Server version: 5.0.26
-- PHP Version: 5.1.6
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `anniversaries`
--
DROP TABLE IF EXISTS `anniversaries`;
CREATE TABLE IF NOT EXISTS `anniversaries` (
`name` varchar(128) NOT NULL,
`anniversary` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=ascii;
--
-- Dumping data for table `anniversaries`
--
INSERT INTO `anniversaries` (`name`, `anniversary`) VALUES ('Dan Tidyer', '1999-03-15'),
('John Laughter', '2004-04-15'),
('Joan Handyer', '2005-08-12'),
('Alfred Neubee', '2006-06-12');
-- --------------------------------------------------------
--
-- Table structure for table `dates`
--
DROP TABLE IF EXISTS `dates`;
CREATE TABLE IF NOT EXISTS `dates` (
`start` date NOT NULL,
`end` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=ascii;
--
-- Dumping data for table `dates`
--
INSERT INTO `dates` (`start`, `end`) VALUES ('2006-07-01', '2006-10-01');
-- --------------------------------------------------------
--
-- Table structure for table `holidays`
--
DROP TABLE IF EXISTS `holidays`;
CREATE TABLE IF NOT EXISTS `holidays` (
`name` varchar(64) NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=ascii;
--
-- Dumping data for table `holidays`
--
INSERT INTO `holidays` (`name`, `date`) VALUES ('New Year''s Day', '2006-01-02'),
('Martin Luther King, Jr. Day', '2006-01-16'),
('Presidents'' Day', '2006-02-20'),
('Good Friday', '2006-04-14'),
('Memorial Day', '2006-05-29'),
('Independence Day', '2006-07-04'),
('Labor Day', '2006-09-04'),
('Thanksgiving', '2006-11-23'),
('Christmas', '2006-12-25'); | |
|
|
| Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time | | | mysql date/time converters Categories : PHP, MySQL, Databases, Date Time | | | How to load a query result into a PHP Array Categories : PHP, Databases, Arrays, MySQL | | | dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | This simple function will take a few arguments and easily set a associative array for each column in a result from a MySQL query Categories : Databases, PHP, MySQL, Arrays | | | create a grid out of <INPUT TYPE=TEXT> then saving to a database. Uses
a 'multi-dimension array', but not really as the array is just one big array
with the index of "[$i][$j]". Have a look at the code and you'll see what I
mean. Categories : PHP, MySQL, Arrays, Databases | | | Data Retrieve from Mysql using AJAX with PHP Categories : PHP, AJAX, Date Time, Databases, MySQL | | | Function for retrieving MySQL enum values into a PHP array.
Categories : PHP, Databases, MySQL, Arrays | | | Phorum, MySQL, Language, UK date format, MySQL UK Date format Categories : PHP, Date Time, Strings, MySQL, Databases | | | List people whose birthdays fall on the current Day and Month
Categories : Databases, Date Time, MySQL, PHP | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields Categories : MySQL, Date Time, PHP, Databases | | | Simple function to return the number of days in a time span between 2 given dates. Categories : PHP, Date Time, MySQL, Databases | | | Count how many weeks in the month have a specified day, such as Mon, Tue, etc. Var avail - number of days - first day name of the month, occurrences of Sun, occurrences of Mon, etc. Allows you to calculate number of working hours exclude Holidays. Categories : Calendar, Date Time, PHP, Databases, MySQL | |
|
|
|