-2

I'm trying to get the current start date of the month and current end date of the month.

It needs to change for each month, so for example for this month I need:

$startMonth = 2014-02-01
$endMonth = 2014-02-28

I'm aware of the mktime PHP page but I can't get my head around it: http://uk3.php.net/mktime

I also need the start day and end day of the current year:

$startYear = 2014-01-01
$endYear = 2014-12-31

I need it in the above format, I can get the previous months using:

$previousmonthStart = date("Y-m-d", mktime(0, 0, 0, date("m"), 0, date("Y")));
$previousmonthEnd = date("Y-m-d", mktime(0, 0, 0, date("m")-1, 1, date("Y")));

Just need to know what changes what.

zvisofer
  • 1,346
  • 18
  • 41
andy
  • 459
  • 2
  • 6
  • 26

1 Answers1

1

Since you want to use mktime()...

I think the first day of the year and the last day of the current year are always (no mktime() needed):

date('Y') . '01-01'; 
date('Y') . '12-31';

For the first and last of the current month with mktime() try this:

$m = (integer) date('n');
$start = date('Y-m-d',mktime(1,1,1,$m,1,date('Y')));
$end   = date('Y-m-d',mktime(1,1,1,++$m,0,date('Y')));

//Considering today is 2014-02-05
echo $start; //2014-02-01
echo $end;   //2014-02-28

mktime wants hour, min, sec, month, day ,year

$m tells mktime to use the current month for the month value and the next 1 tells it to use the first day of that month.

++$m tells it to use the next month and the 0 gets you the day before the first day of the next month which is the last day of the current month.

Example: http://codepad.org/1G7UJNni

Jason
  • 15,017
  • 23
  • 85
  • 116