SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP date calculation

    (1)

    I need to get the time() value at the start of the current quarter.

    Ie. as now is Dec 2008, the system would return the time() value of 1st Oct 2008.

    date("Q"); would be great if it did this - but it doesn't.

    (2)

    I then need to get the time() values for the start of the next 3 quarters
    i.e:
    Jan-mar 2009
    Apr-Jun 2009
    Jul-Sep 2009

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    Are you looking for strtotime()? http://www.php.net/strtotime

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or something like this can be used to work out the current quarter. You can extend the logic to find next quarters as well.
    Code PHP:
    <?php
    //quarter's start on first, fourth, seventh, and tenth month
    $quarterMonths = array(1, 4, 7, 10);
     
    //current quarter
    $currentMonth = date('n');
    $currentQuarter = null;
    foreach($quarterMonths as $key => $value){
      if($value == $currentMonth){
        $currentQuarter = $currentMonth;
        break;    
      }
      elseif($value > $currentMonth) {
      $currentQuarter = $quarterMonths[$key -1];
        break;
      }     
    }
    $currentQuarter = $currentQuarter ? $currentQuarter : 10;
     
    //so the first day of current quarter is
    $timestamp = mktime(0, 0, 0, $currentQuarter, 1, date('Y'));
    print 'Timestamp: '. $timestamp;
    print 'Date: '. date('F d, Y', $timestamp); 
    ?>

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing but an alternate (umm work around?):
    Code php:
    function getQuarters($qNums, $current_date = null){
    	$arrQs 		= array();
    	$current_m	= ($current_date != null) ? (int)date('n', strtotime($date)) : (int)date('n');
    	$current_q 	= (($current_m &#37; 3) == 0) ? ($current_m - 2) : ($current_m - (($current_m % 3) - 1));
    	$curr_q_ts 	= strtotime(date('Y') . "-$current_q-1");
    	$arrQs[] 	= date('Y-m-d', $curr_q_ts);
     
    	for($i = 1; $i <= $qNums; $i++){
    		$arrQs[] = date('Y-m-d', strtotime("+3 months", $curr_q_ts));
    		$curr_q_ts = strtotime("+3 months", $curr_q_ts);
    	}
    	return $arrQs;
    }
    $quarters = getQuarters(3);
    echo '<pre>'; print_r($quarters); echo '</pre>';
    Last edited by Raju Gautam; Dec 7, 2008 at 01:31. Reason: Converted to function and added to pass current date.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •