PHP Snippets
get_month
Information
(PHP Version >= 4.05)Functions used in this snippet: in_array, strtolower, array_search, strtotime.
Description
array get_month( str month, mixed year)
Takes a text month and numeric year and returns the unix timestamps for the very beginning of the month, and very end of the month.
Returns false on failure.
Snippet
<?php
function get_month($month,$year){
$months = array('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
$ret = array();
if(in_array(strtolower($month), $months)){
$ret[] = strtotime($month . ' 1, ' . $year . ' 00:00');
$nextmonth = $months[array_search($month,$months)];
$year = $nextmonth === 'january' ? $year++ : $year;
$ret[] = strtotime($nextmonth . ' 1, ' . $year . ' 00:00 -1 second');
return $ret;
}
return false;
}
?>
Example
get_month Can be used in the following way:
<?php
$time = get_month('january','2007');
echo $time[0] . ' ' . $time[1]; // outputs "1167638400 1169798399"
$time = get_month('july',2000);
echo date('r',$time[0]) . ' ' . date('r',$time[1]);
// outputs "Sat, 1 Jul 2000 00:00:00 -0700 Tue, 25 Jul 2000 23:59:59 -0700"
?>
Updated on Jun 26th at 8 am.
0 Responses to get_month