Jump to content

Archived

This topic is now archived and is closed to further replies.

shoombooltala

PHP & SQL time compare

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

actually, 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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
what version of mysql are you running? i believe that curdate() is only mysql 5+.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.