shoombooltala Posted September 26, 2006 Share Posted September 26, 2006 I'm storing a date in an SQL database. I need to compare that to today's date and time and see if it's more than 24 hours or not. How would I do that?cheers Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 26, 2006 Share Posted September 26, 2006 are you storing it as a DATE datatype? if so, just do something like this:[code]<?php$yesterday = date('Y-m-d', strtotime('yesterday')); // SQL format for yesterday (24 hours ago)$sql = mysql_query("SELECT * FROM table WHERE myDate >= '$yesterday'");?>[/code]hope this helpsactually, after thinking about it, just use SQL to do it more cleanly:[code]SELECT * FROM table WHERE myDate >= DATE_SUB(CURDATE(), INTERVAL 24 HOURS);[/code] Quote Link to comment Share on other sites More sharing options...
shoombooltala Posted September 26, 2006 Author Share Posted September 26, 2006 hello,thanks for your help. i'm trying the second one with the SQL but i get this error:'CURDATE' is not a recognized built-in function name.any suggestions? Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 26, 2006 Share Posted September 26, 2006 what version of mysql are you running? i believe that curdate() is only mysql 5+. Quote Link to comment Share on other sites More sharing options...
shoombooltala Posted September 26, 2006 Author Share Posted September 26, 2006 i'm running SQL Server 2000! i think that may be the problem :( Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 26, 2006 Share Posted September 26, 2006 haha... yes, that could be the problem... try using the first method i posted. if not that, try to find out what the SQL Server equivalent of the DATE_ADD function is for mysql Quote Link to comment Share on other sites More sharing options...
shoombooltala Posted September 26, 2006 Author Share Posted September 26, 2006 [quote author=obsidian link=topic=109538.msg441650#msg441650 date=1159278429]are you storing it as a DATE datatype? if so, just do something like this:[code]<?php$yesterday = date('Y-m-d', strtotime('yesterday')); // SQL format for yesterday (24 hours ago)$sql = mysql_query("SELECT * FROM table WHERE myDate >= '$yesterday'");?>[/code]hope this helps[/quote]ok i'm trying to do this but I don't exactly understand what strtotime('yesterday') does!!i have a php script that gets executed every 10 mins. the point of it is to send a report only ONCE a day. in it i need to compare today's date and time to the record in the database which holds the date and time of the last time an email was sent.if it was yesterday then send the email, update the lasttime sent in the database to the current time so that whole day it wont send another email until tomrrow.any ideas? Quote Link to comment Share on other sites More sharing options...
shoombooltala Posted September 26, 2006 Author Share Posted September 26, 2006 ???!!!!! Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 26, 2006 Share Posted September 26, 2006 ok, well, strtotime() translates the string provided into a UNIX timestamp. the UNIX timestamp is then used by date() to output the date and time in the correct format. usually, you can do a straight comparison in SQL with the results. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.