Page 1 of 1

[SOLVED] Date calculating problems

Posted: Thu Jul 29, 2004 5:08 am
by bluenote
Hi there,

maybe it's the same thing as with my last posting and i just have been coding too long to see the (simple) solution... anyway, here's the problem:

As a part of our CMS, we have an event database which stores the 'general event information', information about the people involved and additional information about where the event is taking place. Now i have to code a script which should extract the event dates from the current and from the next three calendar weeks.

Code: Select all

<?php
$calendar_week = date("W");
global $calendar_week;
?>
With this, the script knows the current calendar week (31), and i can raise this number to fit my needs - BUT: I need the exact date of each Monday (i. e. 2004-07-26) and each Friday (i. e. 2004-07-30) of the specified calendar week to extract the information from the db tables with a query like this:

Code: Select all

<?php

function getCWstartDate ($my problem) { 

...
...

return $CWstartDate}

function getCWendDate ($my problem too) { 

...
...

return $CWendDate}

$cwMonday = substr($CWstartDate, 8, 2);
$cwFriday = substr($CWendDate, 8, 2);
$cwMonth = substr($CWstartDate, 5, 2);
$cwYear = substr($CWstartDate, 0, 4);

SELECT CONCAT(events.day,'.',events.month,'.',events.year) AS date, events.time, events.location, events.mail INTO OUTFILE "/usr/local/Web/shared/tmp/kw_1.txt" FIELDS TERMINATED BY ';' LINES TERMINATED BY "\n" FROM events WHERE events.day BETWEEN '$cwMonday' AND '$cwFriday' AND events.month = '$cwMonth' AND events.year = '$cwYear'  AND (events.location != '' AND events.location != '0') ORDER BY events.time ASC
?>
.

Does anyone have a function for this, or a simple trick to do? It would be a live-saving tip!

Greez,
- bluenote

Posted: Thu Jul 29, 2004 6:36 am
by bluenote
Hello again,

here's what i have as a possible solution:

Code: Select all

<?php
$presentMonday = date("Y",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y")))."-".date("m",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y")))."-".date("d",mktime(0,0,0,date("n"),(date("j")-(7-date("w"))),date("Y")));
$presentFriday = date("Y",mktime(23,59,59,date("n"),(date("j")+(5-date("w"))),date("Y")))."-".date("m",mktime(23,59,59,date("n"),(date("j")+(6-date("w"))),date("Y")))."-".date("d",mktime(23,59,59,date("n"),(date("j")+(5-date("w"))),date("Y")));
?>
This gives me the 'start date' and the 'end date' of the present working week (Mon-Fri, 2004-07-26 -- 2004-07-30).

Posted: Fri Jul 30, 2004 1:15 am
by bluenote
I found a problem in a part of the above "Get the Monday" string:

Code: Select all

<?php
(date("j")-(7-date("w"))
?>
doesn't seem to work under all circumstances; today, the date of this week's Monday was 2004-07-28; yesterday it was 2004-07-26.

Posted: Fri Jul 30, 2004 2:05 am
by bluenote
I found a code snippet which might could do the Job:

Code: Select all

<?php
$week_number= date("W");
		$year =date("Y");
		
		$week_start = (($week_number*7)-8);
		$week_end = ($settimana_start+6);
?>
This gives out the proper Day-Number-of-theYear, which only must be converted into a date ...

Posted: Fri Jul 30, 2004 2:29 am
by feyd
what about:(untested)

Code: Select all

$now = time();
$dayofyear = date('z',$now);
$thatWeekMonday = $dayofyear + 1 - date('w',$now);
$thatWeekFriday = $dayofyear + 5 - date('w',$now);
$startOfYear = mktime(0,0,0,1,1,date('Y',$now));

// timestamps
$mondayMidnight = $startOfYear + $thatWeekMonday * 24 * 60 * 60;
$fridayLastSecond = $startOfYear + ($thatWeekFriday + 1) * 24 * 60 * 60 - 1;

Posted: Thu Aug 05, 2004 2:33 am
by bluenote
Hi,

thanx for your answer and a BIG SORRY fo that i haven't replied it 'till yet - but i was moving.

I have experimented with your suggestions and with my code, and here is the (working) 'alpha release':

Code: Select all

<?php
function startOfWeek () {
	
	$start = date("Y",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y")))."-".date("m",mktime(0,0,0,date("n"),(date("j")-date("w")),date("Y")))."-".date("d",mktime(0,0,0,date("n"),(date("j")-(date("w"))),date("Y")));
	
	return $start;}
	
	$start = startOfWeek();
	
	function endOfWeek () {
		
		$end = date("Y",mktime(23,59,59,date("n"),(date("j")+(5-date("w"))),date("Y")))."-".date("m",mktime(23,59,59,date("n"),(date("j")+(6-date("w"))),date("Y")))."-".date("d",mktime(23,59,59,date("n"),(date("j")+(5-date("w"))),date("Y")));
		
		return $end;}
		
		$end = endOfWeek();
		
		$cw_01_syear = substr($start, 0, 4);
		$cw_01_smonth = substr($start, 5, 2);
		$cw_01_sday = substr($start, 8, 2);
		
		$cw_01_eyear = substr($end, 0, 4);
		$cw_01_emonth = substr($end, 5, 2);
		$cw_01_eday = substr($end, 8, 2);
		
		$rquery01 = "SELECT events.location, CONCAT(events.year,'/',events.month,'/',events.day) AS date, events.time, events.email INTO OUTFILE "/usr/local/Web/shared/tmp/cw_01.txt" FIELDS TERMINATED BY ';' LINES TERMINATED BY "\n" FROM events WHERE (events.day >= '$cw_01_sday' AND events.day <= '$cw_01_eday') AND (events.month >= '$cw_01_smonth' AND events.month <= '$cw_01_emonth') AND (events.year >= '$cw_01_syear' AND events.year <= '$cw_01_eyear') AND (events.location != '' AND events.location != '0') ORDER BY events.weekday ASC, events.day ASC, events.month ASC, events.year ASC, events.time ASC, events.location ASC";
		$rerg01 = MYSQL_QUERY($rquery01) or die(mysql_error());
		
		$sday02 = $cw_01_sday + 7;
		$eday02 = $cw_01_eday + 7;
		
		$start02 = date("Y-m-d", mktime (0,0,0,$cw_01_smonth,$sday02,$cw_01_syear));
		$end02 = date("Y-m-d", mktime (23,59,59,$cw_01_emonth,$eday02,$cw_01_eyear));
		
		$cw_02_syear = substr($start02, 0, 4);
		$cw_02_smonth = substr($start02, 5, 2);
		$cw_02_sday = substr($start02, 8, 2);
		
		$cw_02_eyear = substr($end02, 0, 4);
		$cw_02_emonth = substr($end02, 5, 2);
		$cw_02_eday = substr($end02, 8, 2);
		
		$rquery02 = "SELECT events.location, CONCAT(events.year,'/',events.month,'/',events.day) AS date, events.time, events.email INTO OUTFILE "/usr/local/Web/shared/tmp/cw_02.txt" FIELDS TERMINATED BY ';' LINES TERMINATED BY "\n" FROM events WHERE (events.day >= '$cw_02_sday' AND events.day <= '$cw_02_eday') AND (events.month >= '$cw_02_smonth' AND events.month <= '$cw_02_emonth') AND (events.year >= '$cw_02_syear' AND events.year <= '$cw_02_eyear') AND (events.location != '' AND events.location != '0') ORDER BY events.weekday ASC, events.day ASC, events.month ASC, events.year ASC, events.time ASC, events.location ASC";
		$rerg02 = MYSQL_QUERY($rquery02) or die(mysql_error());
?>
This can be continued for week_03, week_04 etc. So far, my problem seems to be solved - until somebody has an idea to do it better, easier or more efficient ...

By the way, a question to feyd as moderator: how can a topic be flagged as "solved"?

Greetings,
- bluenote

Posted: Thu Aug 05, 2004 3:25 am
by feyd
By the way, a question to feyd as moderator: how can a topic be flagged as "solved"?
by me clicking a little button that says "solved", or you can edit your original post's title/subject