By week and yr i get 1st and last day of week, but... SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Porsche.V
Forum Newbie
Posts: 3
Joined: Mon May 26, 2008 10:49 am

By week and yr i get 1st and last day of week, but... SOLVED

Post by Porsche.V »

By week and yr i get 1st and last day of week:

<?php
$dia='13-08-2008';
list($d, $m, $y) = explode('-', $dia);
$week=date('W',strtotime("$y-$m-$d"));

function week_start_date($wk_num, $yr, $first = 1, $format = 'F d Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}

$sStartDate = week_start_date($week-1, $y);
$sEndDate = date('d-m-Y', strtotime('+6 days', strtotime($sStartDate)));
print "week: $week, start: $sStartDate, end: $sEndDate";
?>

but i get:

week: 33, start: August 11 2008, end: 17-08-2008

and i want:

week: 33, start: 11-08-2008, end: 17-08-2008

i tried to change the format at the function:
$format = 'm-d-Y'

so i could explode, but it gives a:

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970

and it prints the start date correct: start: 08-11-2008 but then i can't process the end date..

can someone help me with this?

thanks a lot
Last edited by Porsche.V on Tue May 27, 2008 7:14 am, edited 1 time in total.
Porsche.V
Forum Newbie
Posts: 3
Joined: Mon May 26, 2008 10:49 am

Re: By week and yr i get 1st and last day of week, but...

Post by Porsche.V »

forget it, solved:

<?php
$dia='13-08-2008';
list($d, $m, $y) = explode('-', $dia);
$week=date('W',strtotime("$y-$m-$d"));

function week_start_date($wk_num, $yr, $first = 1, $format = 'Y-m-d')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}

$sStartDate = week_start_date($week-1, $y);
$sEndDate = date('d-m-Y', strtotime('+6 days', strtotime($sStartDate)));
list($yy, $mm, $dd) = explode('-', $sStartDate);
$primeiro_dia = "$dd-$mm-$yy";
print "week: $week, start: $primeiro_dia, end: $sEndDate";
?>
Post Reply