JustMike Posted December 20, 2013 Share Posted December 20, 2013 Hi, I'm just started to learn PHP and now I encountered a problem. I'm trying to show a message every 24h but I can't figure it out. Here is my code: $link = mysql_connect('localhost', 'root', '');if (!$link) { die('Not connected : ' . mysql_error());}$db_selected = mysql_select_db('gifts', $link);if (!$db_selected) { die ('Can't use foo : ' . mysql_error());}$sel = mysql_query("SELECT * FROM xmas") or die(mysql_error()); $row = mysql_fetch_assoc($sel); $time_now = date('y-m-d h:i:s', time());//var_dump($time_now); if($time_now>$row['date']){ echo "here's your gift "; }else{ echo "next gift tomorrow!"; } I know I should use mysqli or PDO because the mysql will be deprecated, but I started to learn this way and after I get the basics I will change to one of those. How can I solve this? What I'm missing? Thanks in advance for your help! Link to comment Share on other sites More sharing options...
davej Posted December 20, 2013 Share Posted December 20, 2013 Don't you want a cron job for this? Link to comment Share on other sites More sharing options...
JustMike Posted December 20, 2013 Author Share Posted December 20, 2013 (edited) I don't have acces to cron jobs on the server where the website is hosted. I was thinking that I can check if the $row['date']+86400<time() or somthing similar will work but it seems that I can't figure it out.I forgot to mention, the xmas table looks like this: `id` int(11) NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL, `gift` varchar(255) NOT NULL, I think I found a solution, I don't know if this is the best option but here is my new code: I changed the type of date in the database to DATE and here is my new code: $day = date('Y-m-d');$lastGift = $row['date'];$disabled = 0; if ($lastGift <= $day){ $disabled = 1; }if($disabled == 0){ echo "here's your gift ";}else{ echo "next gift tomorrow!";} Edited December 20, 2013 by JustMike Link to comment Share on other sites More sharing options...
davej Posted December 20, 2013 Share Posted December 20, 2013 I could be wrong but I would think that the server admin would prefer to provide you with a cron service rather than having you run a thread continuously. Link to comment Share on other sites More sharing options...
JustMike Posted December 22, 2013 Author Share Posted December 22, 2013 I know, it's a weird thing,but the hosting is free (from a friend with a server home) and I don't use this thing just a few days a year....anyway, I,ll put my full code when I get home...maybe other users needs this thing too. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now