note 110203 added to function.strtotime

From: Date: Sun, 30 Sep 2012 19:00:15 +0000
Subject: note 110203 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-191078@lists.php.net to get a copy of this message
The function of milespickens at gmail dot com calculating quarters dates needs a small fix, in order to work properly at the last day of the quarter, for example 30th September. At the moment it returns null without this fix: <?php function get_quarter( $minus = NULL ) { //put correct begin dates and end dates in Bingo array $bingo = array(); $quarters = array( 1 => array( '01/01/', '03/31/'), 2 => array( '04/01/', '06/30/'), 3 => array( '07/01/', '09/30/'), 4 => array( '10/01/', '12/31/') ); if( $minus ) { $adjust = 3 * $minus; $today = strtotime($adjust . " months", time()); } else { $today = time(); } $year = date("Y", $today); foreach($quarters as $key => $val) { $date1 = strtotime($val[0] . $year); $date2 = strtotime($val[1] . $year); $date2 += (24*60*60); // Add this line to correct timestamp for last day of the quarter if( ($today > $date1) && ($today < $date2)) { $bingo[] = $quarters[$key][0] . $year; $bingo[] = $quarters[$key][1] . $year; } } return $bingo; } $this_quarter = get_quarter( NULL ); $last_quarter = get_quarter( -1 ); $next_quarter = get_quarter( 1 ); var_dump($this_quarter); var_dump($last_quarter); var_dump($next_quarter); ?> We need to add 24 hours for the end date timestamp before compare: $date2 += (24*60*60); // Add this line to correct timestamp for last day of the quarter ---- Server IP: 69.147.83.199 Probable Submitter: 82.137.66.167 ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- https://master.php.net/note/edit/110203 Del: integrated -- https://master.php.net/note/delete/110203/integrated Del: useless -- https://master.php.net/note/delete/110203/useless Del: bad code -- https://master.php.net/note/delete/110203/bad+code Del: spam -- https://master.php.net/note/delete/110203/spam Del: non-english -- https://master.php.net/note/delete/110203/non-english Del: in docs -- https://master.php.net/note/delete/110203/in+docs Del: other reasons-- https://master.php.net/note/delete/110203 Reject -- https://master.php.net/note/reject/110203 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#191078) next »