<?php
//echo date('r', strtotime('last sunday', strtotime('6 April 1991')));
function align_timestamp_to($stamp, $unit, $past){ // timestamp, unit string, start/end
static $modes = array(
'week' => 'last monday',
'month' => 'first day of month',
);
// TODO error out if unknown $unit
$offset = $modes[$unit];
if($past == 'end')$offset = '+1 '.$unit.' '.$offset;
$new = strtotime($offset, $stamp);
}
foreach(array(
array(strtotime('4 Apr 2012'), 'week', 'start'),
array(strtotime('4 Apr 2012'), 'week', 'end'),
) as $test)echo date('r', $test[0]).' aligned '.$test[2].' of '.$test[1].' yields '.date('r', align_timestamp_to($test[0], $test[1], $test[2])).'<br/>';
preferences:
40.97 ms | 402 KiB | 5 Q