Jump to content

Week starting and Ending dates.


Moon-Man.net

Recommended Posts

Hello,
I have a piece of code that is used to select each week's starting date and ending date for a PostgreSQL report. The dates must be starting (Monday) and ending (sunday) for each week in the given month of the given year. ($view_month & $view_year) This workes for EVERY month EXCEPT febuary. where it skips a month.

Could some one please give me a hand?
Ive been killing myslef over it all weekend/last week :(
Cheers,
Nathan

[code]<?PHP
$view_month = 2 ;// Febuary
$view_year = 2005 ; // 2--5
$end_date = date('t', mktime(0, 0, 0, $view_month, 0, $view_year)) ;

echo "<select name='view_day'>" ;

$prvMonth = $view_month - 1 ;
$prvMthName = date('M', mktime(0, 0, 0, $prvMonth, 1, $view_year)) ;
$prvMthDay = date('t', mktime(0, 0, 0, $prvMonth, 1, $view_year)) ;

for ($i = $prvMthDay - 6; $i < $prvMthDay + 1; $i++){
echo "<option value='$i'>i == $i\n" ;
echo date('D', mktime(0, 0, 0, $prvMonth, $i, $view_year)) ;
if (date('D', mktime(0, 0, 0, $prvMonth, $i, $view_year)) == "Mon" ){
// Put into Select Box
$startDay = date('dS F', mktime(0, 0, 0, $prvMonth, $i, $view_year)) ;
$endDay = date('dS', mktime(0, 0, 0, $view_month, $i + 7, $view_year)) ;
$work_date = date('F Y', mktime(0, 0, 0, $view_month, $i, $view_year)) ;
}
}

// 2. Do the month requested
for ($i=1; $i < $end_date; $i++){
if (date('D', mktime(0, 0, 0, $view_month, $i, $view_year)) == "Mon" ){
// Put into Select Box
$startDay = date('dS', mktime(0, 0, 0, $view_month, $i, $view_year)) ;
$endDay = date('dS', mktime(0, 0, 0, $view_month, $i + 6, $view_year)) ;
$work_date = date('F Y', mktime(0, 0, 0, $view_month, $i, $view_year)) ;
if ($endDay > $startDay) {
echo "<option value='$i'>$startDay to $endDay $work_date\n" ;
}
}
}
echo "</select>";
[/code]
Link to comment
Share on other sites

Any use

[code]<?php
$view_month = 2 ;// Febuary
$view_year = 2005 ; // 2--5

$first = mktime (0,0,0,$view_month, 1, $view_year);
$last = date('t', $first);
$wks = ceil($last/7);
$dow1 = (date('w', $first)+6)%7;
$mon = strtotime ("-$dow1 days", $first);

echo "<select name='view_day'>\n";
for ($i=1; $i<=$wks; $i++) {
$sun = strtotime ('+6 days', $mon);
printf ("<OPTION value='$i'>%s to %s</OPTION>\n", date('jS', $mon), date('jS F Y', $sun));
$mon = strtotime ('+7 days', $mon);
}
echo '<select>';
?>[/code]
Link to comment
Share on other sites

That would be perfect, but how does the %s work ETC? And how can i get it to show the brevious month's name? Eg in the provious code it has:
[quote]
30th January to Febuary 5th
6th to 12th January
13th to 19th January
[/quote]

And also the starting date as the select option.
Cheers,
Nathan
Link to comment
Share on other sites

[quote]but how does the %s work[/quote]
http://www.php.net/sprintf

[quote]how can i get it to show the brevious month's name?[/quote]
You could try this if prefered
[code]printf ("<OPTION value='$i'>%s to %s</OPTION>\n", date('jS M', $mon), date('jS M Y', $sun));[/code]

[quote]And also the starting date as the select option.[/quote]
???
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.