note 100390 modified in function.date by danbrown

From: Date: Wed, 13 Oct 2010 22:10:33 +0000
Subject: note 100390 modified in function.date by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173200@lists.php.net to get a copy of this message
Here is a very simple function to get quarterly dates. <?php function quarter($month='',$day='') { if($month != ""){$tm = mktime(0,0,0,$month,$day-1,date('Y'));}else{$tm = date('Y-m-d H:i:s');} $quarter = ceil(date("m", $tm)/3); for($i=1; $i<=4; $i++) { $checkd = mktime(0,0,0,date('m')-$i,date('d'),date('Y')); if(ceil(date("m", $checkd)/3) != "$quarter") { $meani = date("m", $checkd); for($a=1; $a<=31; $a++) { $forman = mktime(0,0,0,$meani,$a,date('Y')); if(ceil(date("m", $forman)/3) == "$quarter") { $first_day = date('d', $forman); break; } } break; } } $current_quarter_dayone = date('Y-m-d H:i:s', mktime(0,0,0,$meani,$first_day,date('Y'))); return $current_quarter_dayone; } ?> To get the current quarterly start date. $quarter_start= quarter(); ************************************** 2010-09-01 00:00:00 To find the start of a particular quarter based on a date, pass the month and day. $quarter_start = quarter('08','30'); $quarter_end = date("Y-m-d H:i:s", strtotime($barter)-86400); Results ********************************** 2010-06-01 00:00:00 2010-08-31 00:00:00 --was-- Here is a very simple function to get quarterly dates. function quarter($month='',$day='') { if($month != ""){$tm = mktime(0,0,0,$month,$day-1,date('Y'));}else{$tm = date('Y-m-d H:i:s');} $quarter = ceil(date("m", $tm)/3); for($i=1; $i<=4; $i++) { $checkd = mktime(0,0,0,date('m')-$i,date('d'),date('Y')); if(ceil(date("m", $checkd)/3) != "$quarter") { $meani = date("m", $checkd); for($a=1; $a<=31; $a++) { $forman = mktime(0,0,0,$meani,$a,date('Y')); if(ceil(date("m", $forman)/3) == "$quarter") { $first_day = date('d', $forman); break; } } break; } } $current_quarter_dayone = date('Y-m-d H:i:s', mktime(0,0,0,$meani,$first_day,date('Y'))); return $current_quarter_dayone; } To get the current quarterly start date. $quarter_start= quarter(); ************************************** 2010-09-01 00:00:00 To find the start of a particular quarter based on a date, pass the month and day. $quarter_start = quarter('08','30'); $quarter_end = date("Y-m-d H:i:s", strtotime($barter)-86400); Results ********************************** 2010-06-01 00:00:00 2010-08-31 00:00:00 http://php.net/manual/en/function.date.php

« previous php.notes (#173200) next »