Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the beginning and ending unix timestamp for a given month and year in php

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).

like image 894
dotty Avatar asked Sep 03 '10 08:09

dotty


People also ask

How do I get the last day of the current month in PHP?

$date = strtotime ( $datestring ); // Last date of current month. $day = date ( "l" , $lastdate );

How do I find the first date and last date of the month?

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.

How can get current year start and end date in PHP?

$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);

How can I get start date and end of month in PHP?

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!


1 Answers

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);
like image 58
Matteo Riva Avatar answered Sep 29 '22 23:09

Matteo Riva