Jump to content


Photo

Timestamp Error


  • Please log in to reply
4 replies to this topic

#1 lukelambert

lukelambert
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 July 2006 - 04:52 AM

I wrote a script to display the days of a selected month in calendar form (4, 5 or 6 rows by 7 columns). I wanted the days of the previous and next months to fill the empty cells on the calendar before and after the days in the selected month.

For example, the calendar for November 2006 would look like this:























































Sun Mon Tue Wed Thu Fri Sat
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2


The display part is working perfectly but now I want to select events from an SQL table that have a timestamp that is greater than or equal to the first day on the calendar at 12:00 am and less than one day after the last day on the calendar at 12:00 am. In other words, I want the events that take place between the beginning and end of the calendar.

Anyway, here is my code...
$month = 11; // Let's say the selected month is November 2006
$year = 2006;

$month_days = array( // The number of days in each month
     1 => 31,
     2 => 28,
     3 => 31,
     4 => 30,
     5 => 31,
     6 => 30,
     7 => 31,
     8 => 31,
     9 => 30,
    10 => 31,
    11 => 30,
    12 => 31
);

if (date("L", mktime(0, 0, 0, 1, 1, $year))) // Determines if it is a leap year
    $month_days[2] = 29;

$day_of_week = date("w", mktime(0, 0, 0, $month, 1, $year)); // The day of the week of the first day of the month

$num_rows = ceil(($month_days[$month] + $day_of_week) / 7); // Number of seven cell rows

$timestamp_begin = date("U", mktime(0, 0, 0, $month, 1, $year)) - $day_of_week * 86400;
/* The timestamp of the first day of the month minus (the number of 
days before the month begins times the number of seconds in a day) */

$timestamp_end = $timestamp_begin + $num_rows * 7 * 86400; 
/* The timestamp of the first day on the calendar plus (the 
number of cells times the number of seconds in a day) */

echo $timestamp_begin . " = " . date("F d, Y H:i:s", $timestamp_begin) . "<br />";
echo $timestamp_end . " = " . date("F d, Y H:i:s", $timestamp_end);

This prints
1162101600 = October 29, 2006 01:00:00
1165125600 = December 03, 2006 00:00:00

On some months it works perfectly and on others it is an hour off on either $timestamp_begin or $timestamp_end. Am I doing something wrong or is this a bug?

I am running PHP Version 5.1.2 on Mac OS X 10.4.7.

#2 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 28 July 2006 - 05:38 AM

You're overcomplicating things. You want to query a table for all entries modified in november?

SELECT * FROM table WHERE timestamp BETWEEN '20061101000000' AND '20061201000000'



#3 lukelambert

lukelambert
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 July 2006 - 06:17 AM

I don't want events only in November. I want from October 29 to December 2. Yes I could SELECT * FROM table WHERE timestamp BETWEEN '20061029000000' AND '20061203000000' but the 'timestamp' column is int(11).

#4 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 28 July 2006 - 07:29 PM

Pfffff. It doesn't matter. The point is; you don't need all these time conversion functions.

Why is your 'timestamp' an INT datatype col, when it's appropiate to store dates in either DATE, DATETIME or TIMESTAMP type cols?

I still believe your making this far more complicated than it needs to be.

SELECT * FROM table WHERE timestamp BETWEEN '20061002000000' AND '20061202000000'

or if col of DATE type;

SELECT * FROM table WHERE timestamp BETWEEN '20061002 AND '20061202'

#5 lukelambert

lukelambert
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 July 2006 - 07:38 PM

The table was created by a third party product. It is already populated with data and I don't intend on altering it. Yes, I agree that your solution would by far be the easiest way, but I am not looking for a new solution. I need to know why my code is not working. If there are 86400 seconds in one day and 3024000 in 35 days shouldn't that math work out?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users