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 Link to comment https://forums.phpfreaks.com/topic/22096-php-sql-time-compare/ 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] Link to comment https://forums.phpfreaks.com/topic/22096-php-sql-time-compare/#findComment-98891 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? Link to comment https://forums.phpfreaks.com/topic/22096-php-sql-time-compare/#findComment-98923 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+. Link to comment https://forums.phpfreaks.com/topic/22096-php-sql-time-compare/#findComment-98929 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 :( Link to comment https://forums.phpfreaks.com/topic/22096-php-sql-time-compare/#findComment-98939 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 Link to comment https://forums.phpfreaks.com/topic/22096-php-sql-time-compare/#findComment-98957 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? Link to comment https://forums.phpfreaks.com/topic/22096-php-sql-time-compare/#findComment-98994 Share on other sites More sharing options...
shoombooltala Posted September 26, 2006 Author Share Posted September 26, 2006 ???!!!!! Link to comment https://forums.phpfreaks.com/topic/22096-php-sql-time-compare/#findComment-99016 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. Link to comment https://forums.phpfreaks.com/topic/22096-php-sql-time-compare/#findComment-99042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.