Jump to content


Photo

Select date range from timestamp with php and mysql


  • Please log in to reply
2 replies to this topic

#1 oli_62

oli_62
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 April 2006 - 03:28 PM

Hello

I want make a database query with a date range (e.g. 01.01.2006-01.03.2006). I set on each Insert statement a timestap. And this select command should list only the entries within this date range.

Any ideas?

Thanks oli

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 18 April 2006 - 03:43 PM

I have this very same thing on my intranet system.

I used the mktime() function.

if you want to search between 2006-18-04 16:40:22 and 2006-19-04 16:40:22 (between now and this time tomorrow) you would use this

<?php
$start = mktime(16, 40, 22, 04, 18, 2006);
$end = mktime(16, 40, 22, 04, 19, 2006);

$query = "SELECT * FROM your_table WHERE time_stamp > $start AND time_stamp < $end";

// etc etc etc
?>

This will select from your_table all entries where the time_stamp field is greater than the timestamp for the $start variable and less than that timestamp for the $end variable.

Hope this helps you on your way.
wisewood: proven fact, I am both wise, and wooden.

#3 oli_62

oli_62
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 18 April 2006 - 06:13 PM

Thanks it seems to work.


I modified it a bit!

<?php
$start = mktime(16, 40, 22, 04, 18, 2006);
$end = mktime(16, 40, 22, 04, 19, 2006);

$query = "SELECT * FROM your_table WHERE time_stamp => $start AND time_stamp =< $end";

// etc etc etc
?>

With the equal before the greather or smaller character lists also the day of the var ($start or $end).

oli






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users