Zend - The PHP Company




Calculators

Add Code


Month Start and End Timestamps  

Type: code fragment
Added by: imposter
Entered: 26/03/2003
Last modified: 04/12/2002
Rating: - (fewer than 3 votes)
Views: 5148
This is a function which I find usefull when I need two timestamps corresponding to the First and Last days of a specified month. Especially usefull when you have dates stored in a database (as timestamps) and you need to SELECT all records BETWEEN two dates.


<?php
/*BEGIN FUNCTION*/
/*Returns an array with two timestamps, one for the start day of the month and
one for the end day. I expanded the month_end array's creation a little bit to make it easier to understand. I'm sure this has been done elsewhere but I couldn't find it when I looked.*/
function get_month_stamp($month$year){
    
//Set month information
    
$month_end = array(
                              
=> 31,
                             
=> 28,
                             
=> 31,
                             
=> 30,
                             
=> 31,
                             
=> 30,
                             
=> 31,
                             
=> 31,
                             
=> 30,
                             
10 => 31,
                             
11 => 30,
                             
12 => 31
                             
);                                           
    
//Select end day of month to work with
    
$end_day $month_end[$month];
    
    
//Make time stamps
    
$start_stamp mktime(000$month1$year);
    
$end_stamp mktime(235959$month$end_day$year);
    
    
//Make array for return
    
$month_array = array('start'=>$start_stamp'end'=>$end_stamp);
    Return 
$month_array
    
}
/*END FUNCTION*/
?>


Usage Example


//Get the current month
$datearray = getdate();
$month = $datearray['mon'];
$year = $datearray['year'];

/*Use the function to get an array containing the two timestamps*/
$my_array = get_month_stamp($month, $year);

echo "Start: ".date("d/m/y", $my_array['start'])." | End: ".date("d/m/y", $my_array['end'])."<br />";

//And here are the timestamps
echo "Start Stamp: ".$my_array['start']." | End Stamp: ".my_array['end'];


Rate This Script





Search



This Category All Categories