Friday, 26 June 2015

Get the Timestamp of First and Last day of Month

// Returns an Array with Timestamp of First and Last day of month
// Receives the specified $month (number of month) and $year
// $year is optional, if it is not specified, uses current year
// From: http://coursesweb.net/php-mysql/
function timeFLDayMonth($month, $year = 0) {
  $tmstp['fd'] = strtotime("$year-$month-01");
  $tmstp['ld'] = strtotime('next month', $tmstp['fd']);
  return $tmstp;
}
- Example:
<?php
// Returns an Array with Timestamp of First and Last day of month
// Receives the specified $month (number of month) and $year
// $year is optional, if it is not specified, uses current year
// From: http://coursesweb.net/php-mysql/
function timeFLDayMonth($month, $year = 0) {
  $tmstp['fd'] = strtotime("$year-$month-01");
  $tmstp['ld'] = strtotime('next month', $tmstp['fd']);
  return $tmstp;
}

// Timestamp of 1st and last day of October (month 10) 2012
$tm10_2012 = timeFLDayMonth(10, 2012);
var_export($tm10_2012);         // array ( 'fd' => 1349042400, 'ld' => 1351724400 )

// Timestamp of 1st and last day of August (month 8), current year
$tm8 = timeFLDayMonth(8);
var_export($tm8);
?>

0 comments:

Post a Comment