puiu91
1/19/2016 - 2:35 PM

date time.php

<?php

$week = date('W');
$year = date('o');

$start = new DateTime();
$start->setISODate($year, $week, 1);
$start->setTime(0, 0, 0);

$end = new DateTime();
$end->setISODate($year, $week, 7);
$end->setTime(23, 59, 59);

//debug($week);
//debug($year);

echo '------------------------------------------------------------------------------------';
echo '<br>';


debug($start);
debug($end);

/**
 * This period last year
 */
echo 'This period last yearD ------------------------------------------------------------------------------------';
echo '<br>';
echo 'Start last ------------------------------------------------------------------------------------';
echo '<br>';

$start_last = new DateTime();

/**
 * SetISODate(year, week, day)
 * See http://php.net/manual/en/function.date.php
 *
 */
$start_last->setISODate(
    $start->format("o") - 1,
    $start->format("W"),
    1
);

echo '+++++++++++++++++';
echo '<br>';
debug($start->format('o'));
debug($start->format('W'));
echo '+++++++++++++++++';
echo '<br>';

debug($start_last);

echo 'End last ------------------------------------------------------------------------------------';
echo '<br>';


$end_last = new DateTime();
$end_last->setISODate(
    $end->format("o") - 1,
    $end->format("W"),
    7
);

debug($end_last);

/**
 * This YTD
 */

echo 'This YTD ------------------------------------------------------------------------------------';
echo '<br>';

$start_ytd = new DateTime();
$start_ytd->setDate($start->format('Y'),1,1);

 debug($start_ytd);

/**
 * Last YTD
 */
echo 'Last YTD ------------------------------------------------------------------------------------';
echo '<br>';


$start_last_ytd = new DateTime();
$start_last_ytd->setDate($start_last->format('Y'),1,1);

debug($start_last_ytd);
<?php

// DateTime object
$DateTime = new DateTime();

// Current year and week
$currentWeek = $DateTime->format('W'); // iso week
$currentYear = $DateTime->format('o'); // current iso year

// Equivalent to...
$currentWeek = date('o');
$currentYear = date('W');