Home     Articles & Projects     Products & Web Services     Forum

Date Time Selector

Hi David,

Is there an easy way to create a date / time selector. I am trying to input a future date/time into a mySQL database so that I can display entries which haven't expired.

Thanks,
Simon

Hi Simon, Rather than use

Hi Simon,

Rather than use MySQL's native DATE/TIME format, I always store them as an INT(11), and in that field store PHP time() values (which is basically the same as the Unix timestamp - seconds since 1st Jan 1970)

This means that it's very easy to do calculations / queries as you describe using the flexible strtotime() function.

For example, if you have a table with an column called "expire", you could select records that have not yet expired as follows:

$sql = "SELECT * FROM mytable WHERE expire < '".time()."'";

Or, to select records that expire in the next 2 weeks:

$sql = "SELECT * FROM mytable WHERE expire BETWEEN '".time()."' AND '".strtotime("+2 Weeks")."'";

Hope this helps!
Cheers,
David.

Hi David, That's great,

Hi David,

That's great, thanks for your help once again.

I would just like to add that to display the date again you would use the date() function

echo date("d M Y",$timestamp);

that would display the date in the format 14 October 2008

Thanks,
Simon
Compare Electricals