PHP Date Range by week and by month

0

Get date range by this week, last week, this month, and last month options by providing offsets.

<?php
function getWeekRange(&$start_date, &$end_date, $offset=0) {
$start_date = '';
$end_date = '';
$week = date('W');
$week = $week - $offset;
$date = date('Y-m-d');

$i = 0;
while(date('W', strtotime("-$i day")) >= $week) {
$start_date = date('Y-m-d', strtotime("-$i day"));
$i++;
}

list($yr, $mo, $da) = explode('-', $start_date);
$end_date = date('Y-m-d', mktime(0, 0, 0, $mo, $da + 6, $yr));
}

function getMonthRange(&$start_date, &$end_date, $offset=0) {
$start_date = '';
$end_date = '';
$date = date('Y-m-d');

list($yr, $mo, $da) = explode('-', $date);
$start_date = date('Y-m-d', mktime(0, 0, 0, $mo - $offset, 1, $yr));

$i = 2;

list($yr, $mo, $da) = explode('-', $start_date);

while(date('d', mktime(0, 0, 0, $mo, $i, $yr)) > 1) {
$end_date = date('Y-m-d', mktime(0, 0, 0, $mo, $i, $yr));
$i++;
}
}

getWeekRange($start, $end);
echo "$start $end";

getMonthRange($start, $end);
echo "$start $end";

?>
Labels:
Loading related posts...

0 comments:

Post a Comment

2010 WEBSITE20. All rights reserved.