Jump to content
Sign in to follow this  
JustMike

Show message every 24 hours

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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 by JustMike

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...