3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function align_timestamp_to($stamp, $unit, $past){ // timestamp, unit string, start/end static $modes = array( 'year' => 'last year', 'week' => 'last monday', 'month' => 'first day of this month', 'hour' => 'last hour', 'minute' => 'last minute', ); // TODO error out if unknown $unit $offset = $modes[$unit]; if($past == 'end')$offset = '+1 '.$unit.' '.$offset.' -1 second'; return strtotime($offset, $stamp); } foreach(array( array(strtotime('4 Apr 2012'), 'year', 'start'), array(strtotime('4 Apr 2012'), 'year', 'end'), array(strtotime('4 Apr 2012'), 'month', 'start'), array(strtotime('4 Apr 2012'), 'month', 'end'), 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/>';
Output for 5.3.0 - 5.6.28, hhvm-3.10.0 - 3.13.2, 7.0.0 - 7.1.0
Wed, 04 Apr 2012 00:00:00 +0200 aligned start of year yields Mon, 04 Apr 2011 00:00:00 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of year yields Tue, 03 Apr 2012 23:59:59 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned start of month yields Sun, 01 Apr 2012 00:00:00 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of month yields Mon, 30 Apr 2012 23:59:59 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned start of week yields Mon, 02 Apr 2012 00:00:00 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of week yields Sun, 08 Apr 2012 23:59:59 +0200<br/>
Output for 5.2.0 - 5.2.17
Wed, 04 Apr 2012 00:00:00 +0200 aligned start of year yields Mon, 04 Apr 2011 00:00:00 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of year yields Tue, 03 Apr 2012 23:59:59 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned start of month yields Thu, 01 Jan 1970 01:00:00 +0100<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of month yields Thu, 01 Jan 1970 01:00:00 +0100<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned start of week yields Mon, 02 Apr 2012 00:00:00 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of week yields Sun, 08 Apr 2012 23:59:59 +0200<br/>
Output for 5.1.0 - 5.1.6
Wed, 04 Apr 2012 00:00:00 +0200 aligned start of year yields Mon, 04 Apr 2011 00:00:00 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of year yields Tue, 03 Apr 2012 23:59:59 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned start of month yields Thu, 01 Jan 1970 01:00:00 +0100<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of month yields Thu, 01 Jan 1970 01:00:00 +0100<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned start of week yields Mon, 02 Apr 2012 00:00:00 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of week yields Mon, 09 Apr 2012 23:59:59 +0200<br/>
Output for 4.3.11 - 4.4.9, 5.0.4 - 5.0.5
Wed, 04 Apr 2012 00:00:00 +0200 aligned start of year yields Mon, 04 Apr 2011 00:00:00 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of year yields Tue, 03 Apr 2012 23:59:59 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned start of month yields Thu, 01 Jan 1970 00:59:59 +0100<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of month yields Thu, 01 Jan 1970 00:59:59 +0100<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned start of week yields Mon, 02 Apr 2012 00:00:00 +0200<br/>Wed, 04 Apr 2012 00:00:00 +0200 aligned end of week yields Mon, 09 Apr 2012 23:59:59 +0200<br/>
Output for 4.3.0 - 4.3.10, 5.0.0 - 5.0.3
Wed, 4 Apr 2012 00:00:00 +0200 aligned start of year yields Mon, 4 Apr 2011 00:00:00 +0200<br/>Wed, 4 Apr 2012 00:00:00 +0200 aligned end of year yields Tue, 3 Apr 2012 23:59:59 +0200<br/>Wed, 4 Apr 2012 00:00:00 +0200 aligned start of month yields Thu, 1 Jan 1970 00:59:59 +0100<br/>Wed, 4 Apr 2012 00:00:00 +0200 aligned end of month yields Thu, 1 Jan 1970 00:59:59 +0100<br/>Wed, 4 Apr 2012 00:00:00 +0200 aligned start of week yields Mon, 2 Apr 2012 00:00:00 +0200<br/>Wed, 4 Apr 2012 00:00:00 +0200 aligned end of week yields Mon, 9 Apr 2012 23:59:59 +0200<br/>