I want to find the timestamp for the first day in a month (say September 1 2010 and 0:00) and the last day in a month (say September 31 23:59).
$date = strtotime ( $datestring ); // Last date of current month. $day = date ( "l" , $lastdate );
To get the first and last day of the current month, use the getFullYear() and getMonth() methods to get the current year and month and pass them to the Date() constructor to get an object representing the two dates.
$year = date('Y') - 1; // Get current year and subtract 1 $start = "January 1st, {$year}"; $end = "December 31st, {$year}"; If you need the timestamp for both those dates: $year = date('Y') - 1; // Get current year and subtract 1 $start = mktime(0, 0, 0, 1, 1, $year); $end = mktime(0, 0, 0, 12, 31, $year);
You can be creative with this. For example, to get the first and last second of a month: $timestamp = strtotime('February 2012'); $first_second = date('m-01-Y 00:00:00', $timestamp); $last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!
If you have those dates as strings you can simply use strtotime(), if you have only partial information you can use mktime().
However, September only has 30 days ;)
Example:
$month = 9;
$year = 2010;
$first = mktime(0,0,0,$month,1,$year);
echo date('r', $first);
$last = mktime(23,59,00,$month+1,0,$year);
echo date('r', $last);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With