views:

35

answers:

3

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

Any ideas how to do this?

A: 

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);
kemp
I don't have these strings, the 2 params passed via the querystring is month and year. From there i need to work on the 1st and last timestamps.
dotty
Ok, edited my answer
kemp
This is how i wrote it however i over come fabrik's problem with $days_in_month = cal_days_in_month(CAL_GREGORIAN,$month,$year);
dotty
Well with my solution you don't have to worry about the last day of the month -- you find the timestamp using the `0` day of the next month. BTW why the downvote?
kemp
A: 

Maybe it can be done simpler but you get the idea:

<?php

$start = mktime(0, 0, 1, $month, 1, $year);
$end   = mktime(23, 59, 00, $month, date('t', $month), $year);

?>
fabrik
+1  A: 

If you are using PHP 5.3 (don't try this with 5.2, date parser works differently there) you could to the speaking:

<?php

$date = "2010-05-10 00:00:00";

$x = new DateTime($date);

$x->modify("last day of this month");
$x->modify("last second");

echo $x->format("Y-m-d H:i:s");
// 2010-05-30 23:59:59
$timestamp = $x->getTimestamp();
edorian
OP need two timestamps not a formatted date if i am right.
fabrik
Edited in the getTimestamp, format was just as a quick pasteable proof that it works. Thanks !
edorian