Simplified data processing
| From: | Patrick Dunford | Date: | Tue, 19 Dec 2000 14:06:58 +0000 |
| Subject: | Simplified data processing | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-31055@lists.php.net to get a copy of this message | ||
The following code gets some dates from a PostgreSQL database and then turns
them into a range of days that covers the first day of the first week to the
last day of the last week.
I suspect the code is unduly complex and could be simplified; opinions? At
this time I'm used to programming in strongly typed languages and not
familiar enough with the tricks possible on languages that aren't strongly
typed, such as PHP or VB.
Here is the code, with comments:
//First task is to find the start and end of the date range in the
schedules
//Firstly, get the data from the database
//Secondly, extract the date range from the first and last records.
$today=date("d/m/Y"); //dd/mm/yyyy
//Find the start of the current week, for query
$todaydow=strftime("%u"); //Day of week for today.
$subtr=$todaydow-1; //Subtract from today for start of week
$firstdow=date("d/m/Y",mktime(0,0,0,date("m"), date("d")-$subtr,
date("Y"))); //First day in the current week
$query=pg_exec($dbconn,"SELECT * FROM Schedule,Occurrence WHERE
Schedule.Scheduleid = Occurrence.Scheduleid AND OccurDate >= '$firstdow'
ORDER BY OccurDate, StartTime");
$numrows=pg_numrows($query);
$row=pg_fetch_array($query,0);
$firstday=$row[occurdate]; //First date returned
$row=pg_fetch_array($query,$numrows-1);
$lastday=$row[occurdate]; //Last date returned
//These dates are strings and must be now split up into separate values
$firstdate=explode("-",$firstday);
if (count($firstdate) != 3)
$firstdate=explode("/",$firstday);
$lastdate=explode("-",$lastday);
if (count($lastdate) != 3)
$lastdate=explode("/",$lastday);
//Convert each string to a timestamp
$firstday=mktime(0,0,0,$firstdate[1],$firstdate[0],$firstdate[2]);
$lastday=mktime(0,0,0,$lastdate[1],$lastdate[0],$lastdate[2]);
//Find the first day in the first week
$fddow=strftime("%u",$firstday);
$fdsubtr=$fddow-1;
//Find the last day in the last week
$lddow=strftime("%u",$lastday);
$ldaddor=7-$lddow;
//Compute the actual start and finish of the ranges
$rangefirst=mktime(0,0,0,date("m",$firstday), date("d",$firstday)-$fdsubtr,
date("Y",$firstday)); //This is the first day
$rangelast=mktime(0,0,0,date("m",$lastday), date("d",$lastday)+$ldaddor,
date("Y",$lastday)); //This is the last day
//Now convert these dates to a difference value
$secsindays=60*60*24;
$rangecount=($rangelast-$rangefirst) / $secsindays + 1;
In the example, the query returns 19/12/00 and 30/1/00 as the two dates at
each end. The code then calculates the first and last dates of the range as
18/12/00 (a Monday) and 4/2/00 (a Sunday) and $rangecount becomes 49 days,
which is in fact correct. This value will then be used in a FOR loop
($i=0;$i<$rangecount;$i++) and on each pass through the loop, the number
will be added as an offset to the first day, 18/12/00, in order to pass
through each of the days in the range in order.
The date format can be guaranteed as dd/mm/yyyy (date("d/m/Y") by the
execution of a Postgre SQL command, "SET DATESTYLE TO 'European' ".
=======================================================================
Patrick Dunford, Christchurch, NZ - http://pdunford.godzone.net.nz/
Truthful lips endure forever, but a lying tongue lasts only a
moment.
-- Proverbs 12:19
http://www.heartlight.org/cgi-shl/todaysverse.cgi?day=20001219
=======================================================================
Created by Mail2Sig - http://pdunford.godzone.net.nz/software/mail2sig/